/*--------------------------------------------------------------*/
/* ACM ICPC 2007-2008                                           */
/* Northeastern European Regional Contest                       */
/* St Petersburg - Barnaul - Tashkent - Batumi, November 28, 200*/
/*--------------------------------------------------------------*/
/* Problem C. Cactus Reloaded                                   */
/*--------------------------------------------------------------*/
/* Solution                                                     */
/*                                                              */
/* Author                Iskander Akishev                       */
/*--------------------------------------------------------------*/
 
import java.io.*;
import java.util.*;

/**
 * Solution for NEERC 2007 problem C (Cactus).
 *
 * @author Iskander Akishev
 */
public class cactus_ia implements Runnable {
	private BufferedReader in;
	private PrintWriter out;
	private int n;
	private ArrayList<Integer>[] aj;
	private int diametr;
	private int[] p;
	private int[] h;
	private int[] phi;
	private int[] color;
	private ArrayList<Integer>[] c;

	private int[] a;
	private int[] next;
	private int[] prev;

	public void run() {
		try {
			solve();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	@SuppressWarnings({"unchecked"})
	private void solve() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);

		StringTokenizer st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		aj = new ArrayList[n];
		c = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			aj[i] = new ArrayList<Integer>();
		}

		int m = Integer.parseInt(st.nextToken());
		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(in.readLine());
			int k = Integer.parseInt(st.nextToken());
			int u = Integer.parseInt(st.nextToken()) - 1;
			for (int j = 1; j < k; j++) {
				int v = Integer.parseInt(st.nextToken()) - 1;
				aj[v].add(u);
				aj[u].add(v);
				u = v;
			}
		}

		a = new int[2 * n];
		next = new int[2 * n];
		prev = new int[2 * n];

		diametr = 0;

		p = new int[n];
		h = new int[n];
		phi = new int[n];
		Arrays.fill(phi, -1);
		color = new int[n];

		p[0] = -1;
		dfs(0);

		out.println(diametr);

		in.close();
		out.close();
	}

	private void dfs(int u) {
		color[u] = 1;
		for (int v : aj[u]) {
			if (v == p[u]) {
				continue;
			}
			if (color[v] == 0) {
				p[v] = u;
				dfs(v);
				if (phi[v] == -1) {
					relaxHeight(u, h[v] + 1);
				} else if (phi[v] == u) {
					c[u].add(h[v]);
					processCycle(u);
				} else {
					c[phi[v]].add(h[v]);
					assert phi[u] == -1 : "Not a cactus!";
					phi[u] = phi[v];
				}
			} else if (color[v] == 1) {
				if (c[v] == null) {
					c[v] = new ArrayList<Integer>();
				} else {
					c[v].clear();
				}
				phi[u] = v;
			}
		}
		color[u] = 2;
	}

	private void processCycle(int u) {
		int k = c[u].size() + 1;
		a[0] = 0;
		for (int i = 0; i < k - 1; i++) {
			a[i + 1] = c[u].get(i);
		}
		for (int i = 0; i < k; i++) {
			a[k + i] = a[i];
		}
		int m = k / 2;

		int h1 = 0;
		for (int i = k - m; i < k; i++) {
			int h2 = k - i + a[i];
			if (h1 < h2) {
				h1 = h2;
			}
		}
		for (int i = k + 1; i <= k + m; i++) {
			int h2 = i - k + a[i];
			if (h1 < h2) {
				h1 = h2;
			}
		}
		relaxHeight(u, h1);

		next[0] = -1;
		prev[0] = -1;
		int head = 0;
		int tail = 0;
		for (int i = 1; i < 2 * k; i++) {
			if ((i - head) > m) {
				head = next[head];
				prev[head] = -1;
			}

			relax((i - head) + a[i] + a[head]);
			
			while (a[i] - a[tail] >= i - tail) {
				tail = prev[tail];
				if (tail == -1) {
					break;
				}
			}
			if (tail == -1) {
				head = i;
				tail = i;
				next[i] = -1;
				prev[i] = -1;
			} else {
				next[tail] = i;
				prev[i] = tail;
				next[i] = -1;
				tail = i;
			}
		}
	}

	private void relaxHeight(int u, int h1) {
		relax(h[u] + h1);
		if (h[u] < h1) {
			h[u] = h1;
		}
	}

	private void relax(int h) {
		if (diametr < h) {
			diametr = h;
		}
	}

	public static void main(String[] args) {
		new Thread(new cactus_ia()).start();
	}
}
