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
50
51
#include <bits/stdc++.h>
#include <queue>
using namespace std;

int n;
vector<int>g[101];
int g_size[101];
int start[101];
int ile = 0;
void read() {
    cin>>n;
    for(int i =1;i<=n;i++){
        int m;
        cin>>m;
        g_size[i]=m;
        for(int j = 0;j<m;j++){
            int a;
            cin>>a;
            g[i].push_back(a);
        }
    }
}

void solve() {
    read();
    int ile = 0;
    int stop = 0;
    do{
        int u =1;
        while(g_size[u]>0){
            int up = g[u][start[u]];
            if(start[u]==0){
                stop++;
            }
            start[u]+=1;
            start[u]%=g_size[u];
            if(start[u]==0){
                stop--;
            }
            u = up;
        }
        ile++;
    } while(stop!=0);
    cout<<ile<<"\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}