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
#!/usr/bin/env python3

import math

n = int(input())
graph = [[] for i in range(n)]

for i in range(n):
	line = list(map(int, input().split()))
	graph[i] = [x - 1 for x in line[1:]]

num_steps = 1
num_shifts = [0 for i in range(n)]
num_shifts[0] = 1

for platform in range(n):
	if not graph[platform]:
		continue

	g = math.gcd(num_shifts[platform], len(graph[platform]))
	mul_coef = len(graph[platform]) // g
	num_steps *= mul_coef
	for i in range(platform, n):
		num_shifts[i] *= mul_coef

	shifts_per_next = num_shifts[platform] // len(graph[platform])
	for next_platform in graph[platform]:
		num_shifts[next_platform] += shifts_per_next

print(num_steps)