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
53
54
55
56
57
58
59
60
61
62
63
64
65
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    
    vector<vector<int> > w;
    vector<int> nr;
    
    for(int i=0; i<n; i++){
        int m;
        cin >> m;
        vector<int> wm;
        for(int j=0; j<m; j++){
            int a;
            cin >> a;
            wm.push_back(a);
        }
        nr.push_back(wm.size()-1);
        w.push_back(wm);
    }
    
    int r=0;
    bool cnd = false;
    while(!cnd){
        r++;
        
        int cur_w = 0;
        
        // Przeprocesuj
        while(w[cur_w].size()!=0){
            nr[cur_w]+=1;
            if(nr[cur_w]>=w[cur_w].size()){
                nr[cur_w] = 0;
            }
            cur_w = w[cur_w][nr[cur_w]]-1;
        }        
        cnd = true;
        // Sprawdź
        for(int i=0; i<n; i++){
            if((nr[i]!=-1)&&(nr[i]!=w[i].size()-1)){
                cnd = false;
            }
        }
        
        
    }
    
    cout << r;
    
    
    
}