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
52
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,t[102][102],dl[102],wsk[102],vis[102];
int main()
{
    cin>>n;
    vis[1]=1; ll ok=0;
    for(ll i=1; i<=n; i++){
        ll k; cin>>k;
        dl[i]=k;
        wsk[i]=1;
            if(k>1 && vis[i]==1)
                ok=1;
            for(int j=1; j<=k; j++){
                cin>>t[i][j];
                if(vis[i]==1)
                    vis[t[i][j]]=1;
            }
    }
    if(!ok){
        cout<<1;
        return 0;
    }
    ll k=n,res=0;
    while(k!=n || res==0){
        res++;
        ll i=1;
        while(dl[i]){
            ll j=t[i][wsk[i]];
            wsk[i]++;
            if(wsk[i]==2)
                k--;
            if(wsk[i]>dl[i]){
                wsk[i]=1;
                k++;
            }
            
            i=j;
        }
    }
    cout<<res;
    return 0;
}