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

n = int(input())
adj = [[] for _ in range(n+1)]
deg = [0] * (n+1)
deg[n] = 1

for v in range(n):
    out = [int(x) for x in input().split(' ')][1:] or [n+1]
    for u in out:
        adj[u-1].append(v)
    deg[v] = len(out)

iters, rots = [1] * (n+1), [0] * (n+1)
iters[0] = deg[0]
rots[0] = 1

for v in range(1, n+1):
    base = math.lcm(*(iters[u] for u in adj[v]))
    visits = sum(rots[u] * base // iters[u] for u in adj[v])
    mul = deg[v] // math.gcd(visits, deg[v])
    iters[v] = base * mul
    rots[v] = visits * mul // deg[v]

print(iters[n])