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<cstdio>
#include<vector>

using namespace std;

#define N 105

vector<int> krawedzie[N];
int stan[N];

bool poczatkowy(int n) {
	for (int i = 1; i <= n; i++) {
		if (stan[i] != 0) return false;
	}

	return true;
}

int main() {
	int n, r, l;
	scanf("%d\n", &n);

	for (int i = 1; i <= n; i++) {
		scanf("%d", &r);

		for (int j = 0; j < r; j++) {
			scanf("%d", &l);
			krawedzie[i].push_back(l);
		}
	}

	unsigned long long result = 0;

	do {
		result++;

		int current = 1;
		while (krawedzie[current].size() > 0) {
			int new_current = krawedzie[current][stan[current]];
			stan[current] = (stan[current] + 1) % krawedzie[current].size();
			current = new_current;
		}

	} while(! poczatkowy(n));

	printf("%llu", result);

	return 0;
}