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
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
using namespace std;


void printVector(vector<pair<int, vector<int> > > platforms) {
	for (int i = 0; i < platforms.size(); i++) {
		cout << platforms[i].first << ": ";
		for (int j = 0; j < platforms[i].second.size(); j++) {
			cout << platforms[i].second[j] << " ";
		}
		cout << endl;
	}
}

int main() {
	std::ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<pair<int, vector<int> > > platforms;
	
	int platformSize, tmp, last = 0;
	for (int i = 0; i < n; i++) {
		cin >> platformSize;
		vector<int> platform;
		if (platformSize > 0) {
			for (int j = 0; j < platformSize; j++) {
				cin >> tmp;
				platform.push_back(tmp - 1);
			}
		} else {
			platform.push_back(-1);
			++last;
		}
		
		platforms.push_back(make_pair(0, platform));
	}
	
	int reset = 1, counter = 0, tmpNext;
	bool first = true;
	
	while (reset > 0) {
		if (first) {
			first = false;
			--reset;
		}

		++counter;
		int next = platforms[0].second[platforms[0].first];
		
		
		if (platforms[0].first == 0) {
			++reset;
		}
		
		
		platforms[0].first += 1;
		platforms[0].first %= platforms[0].second.size();
		if (platforms[0].first == 0) {
			--reset;
		}
		
		while (next > -1) {
			tmpNext = next;
			
			if (platforms[tmpNext].first == 0) {
				++reset;
			}
			
			next = platforms[next].second[platforms[next].first];
			platforms[tmpNext].first = (platforms[tmpNext].first + 1) % (platforms[tmpNext].second.size());
			
			if (platforms[tmpNext].first == 0) {
				--reset;
			}
		}
	}
	
	
	cout << counter;
	return 0;
}