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
import math

n = int(input())

H = [[] for i in range(n+1)]
R = [0 for i in range(n+1)]
vis = [0 for i in range(n+1)]
H[1].append((0,0))
vis[0] = R[0] = 1

res = 1
for i in range(1, n+1):
    adj = list(map(int, input().split()))
    m = adj[0]
    R[i] = m

    for j in range(1, m+1):
        H[adj[j]].append((i,j-1))
    
    for (j,s) in H[i]:
        vis[i] += vis[j] // R[j] + ((s < vis[j] % R[j]) and 1 or 0)

    m = max(m, 1)
    pos = vis[i] % m

    turns = m // math.gcd(m, pos)
    res *= turns
    for j in range(1, i+1):
        vis[j] *= turns

print(res)