program level2013;
var ans,i,j,k,l,m,n,t,r:integer;
w:array[0..1000,0..1000]of boolean;
s,into,q:array[0..1000]of integer;
finish:boolean;
begin
assign(input,'level2013.in');reset(input);
assign(output,'level2013.out');rewrite(output);
readln(n,m);
for i:=1 to m do begin
read(t);
for j:=1 to t do read(s[j]);
for j:=1 to t do begin
if j<>1 then for k:=s[j-1]+1 to s[j]-1 do
for l:=1 to t do if not w[k,s[l]] then begin
w[k,s[l]]:=true;
inc(into[s[l]]);
end;
end;
readln;
end;
while not finish do begin
finish:=true;
inc(ans);
fillchar(q,sizeof(q),0);
r:=0;
for i:=1 to n do if into[i]=0 then begin into[i]:=-1;q[r]:=i;inc(r);end;
for i:=0 to r-1 do
for j:=1 to n do if w[q[i],j] then begin
finish:=false;
dec(into[j]);
end;
end;
writeln(ans);
close(input);
close(output);
end.