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

n = input()
n = int(n)
ans = [0] * n
d = [0] * n
g = []
for i in range(n):
    g.append([])
for i in range(n):
    line = input().split()
    d[i] = int(line[0])
    for j in range(d[i]):
        k = int(line[j + 1]) - 1
        g[k].append(i)
ans[0] = 1
for i in range(1, n):
    for v in g[i]:
        if ans[v] == 0:
            continue
        kek = d[v] // math.gcd(d[v], ans[v])
        if kek != 0:
            for j in range(0, i):
                ans[j] *= kek
    for v in g[i]:
        if ans[v] != 0:
            ans[i] += ans[v] // d[v]
print(ans[0])