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

n = int(input())
ans = [1] * (n+1)
G = []
for i in range(n+1):
    G.append([])
deg = [0] * (n+1)

for i in range(n):
    line = input()
    line = line.split()
    r = int(line[0])
    deg[i+1] = r
    for j in range(1, r+1):
        a = int(line[j])
        G[a].append(i+1)

if deg[1] == 0:
    print(1)
    quit()

for v in range(1, n+1):
    if deg[v] == 0:
        continue
    sum = 0
    if v == 1:
        sum = 1
    for u in G[v]:
        sum += ans[u]
    k = math.gcd(sum, deg[v])
    k = deg[v] // k
    for i in range(1, v):
        ans[i] *= k
    ans[v] = (sum*k) // deg[v]

print(ans[1]*deg[1])