#include<bits/stdc++.h>
using namespace std;
const int Max_S=1e3+10;
const int Size=1e5;
int N,M,Ans;
int S[Max_S],G[Size];
int A[Max_S][Max_S];
int l[Max_S][Max_S],Cnt[Max_S];
struct node{int u,v;};
inline int read(){
int x=0,f=1;char st=getchar();
while(st<'0'||st>'9'){if(st=='-') f=-1;st=getchar();}
while(st>='0'&&st<='9') x=x*10+st-'0',st=getchar();
return x*f;
}
void Bfs(int num){
queue<node>q;
q.push({num,1});
while(!q.empty()){
node tmp=q.front();
q.pop();
for(int i=1;i<=N;i++){
if(l[i][tmp.u]==1&&tmp.v+1>G[i]){
G[i]=tmp.v+1;
q.push({i,tmp.v+1});
Ans=max(Ans,G[i]);
}
}
}
}
int main(){
N=read();M=read();
for(int i=1;i<=M;i++){
S[i]=read();
queue<int>Lower;
A[i][1]=read();
for(int j=2;j<=S[i];j++){
A[i][j]=read();
for(int k=A[i][j-1]+1;k<A[i][j];k++) Lower.push(k);
}
while(!Lower.empty()){
int j=Lower.front();Lower.pop();
for(int k=1;k<=S[i];k++){
if(l[A[i][k]][j]==0){l[A[i][k]][j]=1;Cnt[A[i][k]]++;}
}
}
}
for(int i=1;i<=N;i++){
if(Cnt[i]==0){G[i]=1;Bfs(i);}
}
cout<<Ans<<endl;
return 0;
}