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
n = int(input().strip())

def gcd(a, b):
    while b != 0:
        c = a % b
        a = b
        b = c
    return a

def lcm(a, b):
    return a * (b // gcd(a, b))

def sm(a1, b1, a2, b2):
    a = a1 * b2 + a2 * b1
    b = b1 * b2
    c = gcd(a, b)
    a = a // c
    b = b // c
    return (a, b)

vals = [(0, 1)] * n

vals[0] = (1, 1)

res = 1

for i in range(n):
    ip = input()
    (c, d) = vals[i]
    res = lcm(res, d)
    if ip == '0':
        continue
    if c == 0:
        continue
    vs = [int(j) for j in ip.split()]
    d *= vs[0]
    x = gcd(c, d)
    c = c//x
    d = d//x
    if vs[0] > 1:
        res = lcm(res, d)
    for j in vs[1:]:
        (a, b) = vals[j - 1]
        vals[j - 1] = sm(a, b, c, d)

print(res)