#include<bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
const int N = 1000010;
queue<PII>q;
vector<int> g[N];
int cnt[N], n, m, x, y, ans = 0, sum = 0, a[N], k = 1, is[N];
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++){
scanf("%d", &x);
memset(is, 0, sizeof is);
memset(a, 0, sizeof a);
for(int j = 1; j <= x; j ++)
cin >> a[j], is[a[j]] = 1;
for(int j = 1; j <= n; j ++ )
if(!is[j]) for(int k = 1; k <= x; k ++ )
g[j].push_back(a[k]), cnt[a[k]] ++ ;
}
for(int i = 1; i <= n; i++)
if(!cnt[i]) q.push(make_pair(i, 1));
while(q.size()){
auto u = q.front();
q.pop();
for(int i = 0; i < g[u.first].size(); i++){
cnt[g[u.first][i]]--;
if(!cnt[g[u.first][i]]) q.push(make_pair(g[u.first][i], u.second + 1)), k = max(k, u.second + 1);
}
}
cout << k;
return 0;
}
有大佬麻烦看一下