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
83
#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> find_track(std::vector<std::vector<int>>& description, std::vector<int>& indexes)
{
	std::vector<int> result;

	int track = 0;

	while (track < description.size())
	{
		if (description.at(track).empty()) break;

		int value = description.at(track).at(indexes.at(track));
		result.push_back(value);
		indexes.at(track) = (indexes.at(track) + 1) % description.at(track).size();
		track = value - 1;
		
	}

	return result;
}

bool check_all_zeros(std::vector<int> indexes)
{
	for (int i = 0; i < indexes.size(); i++)
	{
		if (indexes.at(i) != 0) return false;
	}
	return true;
}

int main()
{
	int platforms;

	std::cin >> platforms;

	std::vector<std::vector<int>> description;

	for (int i = 0; i < platforms; i++)
	{
		int size = 0;
		std::cin >> size;

		std::vector<int> conveyors;

		for (int j = 0; j < size; j++)
		{
			int input = 0;
			std::cin >> input;

			conveyors.push_back(input);
		}

		description.push_back(conveyors);
	}

	std::vector<int> indexes;

	for (int i = 0; i < platforms; i++)
	{
		indexes.push_back(0);
	}

	std::vector<int> first_position = find_track(description, indexes);

	int min_suitcase_to_reset = 0;
	std::vector<int> last_indexes;
	std::vector<int> found_position;

	do
	{
		last_indexes = indexes;
		min_suitcase_to_reset++;
		found_position = find_track(description, indexes);
	} while (!(found_position == first_position && check_all_zeros(last_indexes)));

	std::cout << min_suitcase_to_reset;

	return 0;
}