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

public class cactus_md implements Runnable {
	private BufferedReader in;
	private PrintWriter out;

	public void run() {
		try {
			solve();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	int n;
	ArrayList<Integer>[] nei;
	int[] color;
	int[] phi;
	int[] tail;
	int[] par;
	ArrayList<Integer>[] cyc;
	int[] a;
	int answer = 0;
	
	private void addEdge(int a, int b) {
		nei[a].add(b);
		nei[b].add(a);
	}

	private void dfs(int v) {
		color[v] = 1;
		for (int u : nei[v]) {
			if (u == par[v])
				continue;
			if (color[u] == 0) {
				par[u] = v;
				dfs(u);
				if (phi[u] == -1) {
					// take tail[u]
					relaxTail(v, tail[u] + 1);
				} else if (phi[u] == v) {
					// process cycle
					processCycle(v);
				} else {
					// remember cycle
					phi[v] = phi[u];
				}
			} else if (color[u] == 1) {
				// remember cycle
				phi[v] = u;
			}
		}
		if (phi[v] >= 0) {
			cyc[phi[v]].add(v);
		}
		color[v] = 2;
	}

	private void relaxTail(int v, int newTail) {
		relaxAnswer(tail[v] + newTail);
		tail[v] = Math.max(tail[v], newTail);
	}

	private void relaxAnswer(int path) {
		answer = Math.max(answer, path);
	}

	private void processCycle(int v) {
		cyc[v].add(v);
		System.out.println(cyc[v]);
		int len = cyc[v].size();
		int ctail = 0;
		for (int i = 0; i < len; i++) {
			a[i] = tail[cyc[v].get(i)];
		}
		for (int i = 0; i < len - 1; i++) {
			int p1 = Math.min(len - 1 - i, i + 1);
			ctail = Math.max(ctail, p1 + a[i]);
		}
		int sz = len / 2;
		set.clear();
		for (int i = 0; i < sz; i++) {
			set.add(i - a[i]);
		}
		for (int j = 0; j < len; j++) {
			int i = j + sz;
			int min = set.getMinimum();
			relaxAnswer(i - min + a[i % len]);
			set.remove(j - a[j]);
			set.add(i - a[i % len]);
		}
		// clear ArrayList
		cyc[v].clear();
		relaxTail(v, ctail);
	}
	
	MultiSet set = new MultiSet();
	
	class MultiSet {
		TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();

		public void clear() {
			tm.clear();
		}

		public int getMinimum() {
			return tm.firstKey();
		}

		public void remove(int v) {
			int am = tm.get(v) - 1;
			if (am == 0)
				tm.remove(v);
			else
				tm.put(v, am);
		}

		public void add(int v) {
			int am = 0;
			if (tm.containsKey(v))
				am = tm.get(v);
			tm.put(v, am + 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());
		nei = new ArrayList[n];
		cyc = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			nei[i] = new ArrayList<Integer>();
			cyc[i] = new ArrayList<Integer>();
		}
		int m = Integer.parseInt(st.nextToken());
		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(in.readLine());
			int l = Integer.parseInt(st.nextToken());
			int prev = -1;
			for (int j = 0; j < l; j++) {
				int next = Integer.parseInt(st.nextToken()) - 1;
				if (j > 0) {
					addEdge(prev, next);
				}
				prev = next;
			}
		}
		color = new int[n];
		phi = new int[n];
		tail = new int[n];
		par = new int[n];
		a = new int[n];
		par[0] = -1;
		Arrays.fill(phi, -1);
		dfs(0);
		out.println(answer);
		in.close();
		out.close();
	}
	
	public static void main(String[] args) {
		new Thread(new cactus_md()).start();
	}

}
