1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# include <bits/stdc++.h>
typedef int ll;
using namespace std;
const ll inf = 1e9 + 7;
ll n , m , k , ans = 0 , q;
vector<vector<ll> >g(105);
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
          ll sz;
          cin >> sz;
          for (int j = 0; j < sz; j++) {
                ll to;
                cin >> to;
                g[i].push_back(to);
          }
    }
    bool found = 0;
    vector<ll>cur(105 , 0);
    ll kol = 0;
    while (!found) {
        ll v = 1;
        while (g[v].size() > 0) {
               ll next_v = g[v][cur[v]];
               cur[v] = (cur[v] + 1) % g[v].size();
               v = next_v;
        }
        kol++;
        bool all_0 = 1;
        for (int i = 1; i <= n; i++) {
               if (cur[i] != 0) {
                 all_0 = 0;
                 break;
               }
        }
        if (all_0)found = 1;
    }
    cout << kol;
}
/*
4
2827 1074 2431 1152
2900 1968 2994 1633



*/