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
def GCD(a,b):
    c = 0
    while b!=0:
        c = b
        b = a%b
        a = c
    return a

def main():
    ans = 1
    q = 0
    gcd = 0
    n = int(input())
    l = [0 for i in range(1, n + 1)]
    m = [1 for i in range(1, n + 1)] 
    for i in range(1, n + 10):
        l.append(0)
        m.append(1)
    l[1] = 1;
    for i in range(1,n+1):
        xd = list(map(int,input().split()))
        q = xd[0]
        pom = q
        if q > 0:
            gcd = GCD(l[i], pom)
            pom /= gcd
            l[i] /= gcd
            m[i] *= pom
        if l[i] > 0:
            ans = ans / GCD(ans, m[i]) * m[i];
        for j in range(1, q + 1):
            a = xd[j]
            al = l[i]
            am = m[i]
            if al == 0:
                continue
            al = al * (m[a] / GCD(am, m[a]));
            l[a] = l[a] * (am / GCD(am, m[a]));
            l[a] = l[a] + al; 
            m[a] = m[a] * (am / GCD(am, m[a]));
            gcd = GCD(l[a], m[a]) 
            l[a] = l[a] / gcd; 
            m[a] = m[a] / gcd;
    print(int(ans))

main()