#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 2005, M = 2000005;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
int d[N], q[N], dist[N];
bool st[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
d[b] ++ ;
}
void topsort() {
int hh = 0, tt = -1;
for (int i = 1; i <= n + m;i ++ ){
if (!d[i]) q[ ++ tt] = i;
}
while (hh <= tt) {
int t = q[hh ++ ];
for (int i = h[t];~i;i = ne[i]) {
int j = e[i];
if ( -- d[j] == 0) q[ ++ tt] = j;
}
}
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
for (int i = 1;i <= m;i ++ ) {
memset(st, 0, sizeof st);
int start = n, end = 1;
int k; scanf("%d", &k);
for (int j = 1;j <= k;j ++ ){
int stop; scanf("%d", &stop);
start = min(start, stop);
end = max(end, stop);
st[stop] = true;
}
int ver = n + i;
for (int i = start;i <= end;i ++ ) {
if(st[i]) add(ver, i, 1);
else add(i, ver, 0);
}
}
topsort();
for (int i = 1;i <= n;i ++ ) dist[i] = 1;
for (int i = 0;i < n + m;i ++ ){
int t = q[i];
for (int j = h[t];~j;j = ne[j]){
int k = e[j];
if (dist[k] < dist[t] + w[j]) dist[k] = dist[t] + w[j];
}
}
int res = 0;
for (int i = 1;i <= n;i ++ ) res = max(res, dist[i]);
printf("%d", res);
return 0;
}