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
66
67
68
69
70
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;

#define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b))
using ll = long long;
const int INF = 1e9;

const int MAX_N = 100;
vector<int> g[MAX_N];
int state[MAX_N];
int n;

bool verify() {
    for (int i = 0; i < n; ++i) {
        if (state[i] != 0) return false;
    }
    return true;
}

void step() {
    int i = 0;
    while (!g[i].empty()) {
        int next_i = g[i][state[i]];
        state[i] += 1;
        state[i] %= g[i].size();
        i = next_i;
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int k;
        scanf("%d", &k);
        for (int j = 0; j < k; ++j) {
            int v;
            scanf("%d", &v);
            g[i].push_back(v-1);
        }
    }

    for (int i = 0; i < n; ++i) {
        state[i] = 0;
    }

    int res = 0;
    while (true) {
        step();
        res += 1;
        if (verify()) {
            break;
        }
    }
    printf("%d\n", res);

    return 0;
}