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
import math
n = int(input())

G = [[]] * (n + 1)

for i in range(1, n + 1):
    # l = input().split()
    l = list(map(int, input().split()))
    G[i] = l[1:]

vis = [False] * (n + 1)
vis[1] = True

for i in range(1, n + 1):
    if not vis[i]:
        G[i] = []
        continue

    for j in G[i]:
        vis[j] = True

res = max(1, len(G[1]))

while True:
    poprawne = True
    ile = [0] * (n + 1)
    ile[1] = res

    for i in range(1, n + 1):
        if len(G[i]) == 0:
            continue

        if ile[i] % len(G[i]) != 0:
            poprawne = False
            tmp = ile[i] * len(G[i]) // math.gcd(ile[i], len(G[i]))
            res *= tmp // ile[i]
            break

        for j in G[i]:
            ile[j] += ile[i] // len(G[i])

    if poprawne:
        break

print(res)