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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def nwd(a, b):
    if b == 0:
        return a
    return nwd(b, a % b)

def nww(a, b):
    return (a * b) // nwd(a, b)

def reduce(a):
    d = nwd(a[0], a[1])
    a[0] = a[0] // d
    a[1] = a[1] // d
    return a

def add(a, b):
    M = nww(a[1], b[1])
    L = a[0] * (M // a[1]) + b[0] * (M // b[1])
    c = reduce([L, M])
    return c

n = int(input())
deg = []
prev = {}

for i in range(n):
    prev[i] = []

for i in range(n):
    line = input().strip().split()
    d = int(line[0])
    deg.append(d)
    for k in range(1, len(line)):
        x = int(line[k]) - 1
        prev[x].append(i)

# for i in range(n):
#   print(f'prev[{i}] = {prev[i]}')

a = [[1, 1]]

for i in range(1, n):
    a.append([0, 1])
    if len(prev[i]) > 0 and deg[i] > 0:
        for j in prev[i]:
            a[i] = add(a[i], a[j])
        a[i][1] *= deg[i]
        a[i] = reduce(a[i])

# for i in range(n):
#     print(f'a[{i}] = {a[i]}')

res = 1
for i in range(1, n):
    res = nww(res, a[i][1])

if (res * deg[0]) == 0:
    print(1)
else:
    print(res * deg[0])