1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 using namespace std;
5
6 #define maxm 100010
7 #define maxn 50010
8 int n,m,cnt,toit[maxm*2],side[maxn],next[maxm*2],f[maxn];
9 int ans,dfn[maxn],low[maxn],fa[maxn],a[2*maxn],q[2*maxn];
10
11 inline void add(int a,int b) { next[++cnt] = side[a]; toit[cnt] = b; side[a] = cnt; }
12
13 inline void ins(int a,int b) { add(a,b); add(b,a); }
14
15 inline int read()
16 {
17 int x=0,f=1;char ch=getchar();
18 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
19 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
20 return x*f;
21 }
22
23 inline void dp(int root,int last)
24 {
25 int nn = 0;
26 while (last != root) a[++nn] = last,last = fa[last];
27 a[++nn] = root;
28 for (int i = 1;i * 2 <= nn;++i) swap(a[i],a[nn-i+1]);
29 for (int i = 1;i <= nn;++i) a[i] = f[a[i]],a[i+nn] = a[i];
30 int h = 0,t = 0;
31 q[++t] = 1;
32 for (int i = 2;i <= nn * 2;++i)
33 {
34 while (h != t && i-q[h+1]>nn/2) ++h;
35 ans = max(ans,i - q[h+1] + a[i] + a[q[h+1]]);
36 while (h != t && a[q[t]] - q[t]<= a[i] - i) --t;
37 q[++t] = i;
38 }
39 for (int i = 2;i <= nn;++i)
40 f[root] = max(f[root],a[i] + min(i-1,nn+1-i));
41 }
42
43 inline void dfs(int now)
44 {
45 dfn[now] = low[now] = ++cnt;
46 for (int i = side[now];i;i = next[i])
47 {
48 if (toit[i] == fa[now]) continue;
49 if (!dfn[toit[i]]) fa[toit[i]] = now,dfs(toit[i]);
50 low[now] = min(low[toit[i]],low[now]);
51 if (dfn[now] < low[toit[i]]) ans = max(ans,f[now]+f[toit[i]]+1),f[now] = max(f[now],f[toit[i]]+1);
52 }
53 for (int i = side[now];i;i = next[i])
54 if (toit[i] != fa[now]&&dfn[toit[i]]>dfn[now]&&fa[toit[i]] != now) dp(now,toit[i]);
55 }
56
57 int main()
58 {
59 freopen("1023.in","r",stdin);
60 freopen("1023.out","w",stdout);
61 n = read(); m = read(); int u,v,i,j,k;
62 for (i = 1;i <= m;++i)
63 {
64 k = read(); u = read();
65 for (j = 1;j < k;++j) v = read(),ins(u,v),u = v;
66 }
67 cnt = 0;
68 dfs(1);
69 printf("%d",ans);
70 fclose(stdin); fclose(stdout);
71 return 0;
72 }