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
// Autor: Adam Szymaszkiewicz
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main()
{
	int n;
	cin >> n;
	vector<vector<int>> v(n);
	for (int i = 0; i < n; i++)
	{
		int r;
		cin >> r;
		v[i] = vector<int>(r);
		for (int j = 0; j < r; j++)
		{
			cin >> v[i][j];
			v[i][j]--;
		}	
	}
	LL wyn = 1;
	vector<int> w(n);
	while (1)
	{
		int poz = 0;
		while (v[poz].size() != 0)
		{
			int old_poz = poz;
			poz = v[poz][w[poz]];
			w[old_poz] = (w[old_poz]+1)%v[old_poz].size();
		}

		bool flag = 1;
		for (int i = 0; i < n; i++)
			if (w[i] != 0)
				flag = 0;
		if (flag)
			break;
		wyn++;
	}
	cout << wyn << "\n";
}