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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Author: Bartek Knapik

gcds = {}

def gcd(a, b):
    if b < a:
        a, b = b, a 
        
    if a == 0:
        return b

    if (a, b) in gcds.keys():
        return gcds[(a, b)]
    
    org_a = a
    org_b = b
    
    i = 0
    while a & 1 == 0:
        a >>= 1
        i += 1
    
    j = 0
    while b & 1 == 0:
        b >>= 1
        j += 1
        
    k = min(i, j)

    while True:
        if b < a:
            a, b = b, a
        b -= a
        
        if b == 0:
            gcds[(org_a, org_b)] = a << k
            return a << k
        
        while b & 1 == 0:
            b >>= 1


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


def simplify(a):
    d = gcd(a[0], a[1])
    return (a[0] // d, a[1] // d)


def add(a, b):
    res = (a[0]*b[1] + a[1]*b[0], a[1]*b[1])
    return simplify(res)


def mul(a, b):
    res = (a[0]*b[0], a[1]*b[1])
    return simplify(res)



n = int(input())
outgoing = []
sizes = []
factors = []

for _ in range(n+1):
    outgoing.append([])
    sizes.append(0)
    factors.append((0, 1))
    
for i in range(1, n+1):
    l = input()
    ll = [int(el) for el in l.split(' ')]
    sizes[i] = ll[0]

    for j in range(1, len(ll)):
        outgoing[i].append(ll[j])

factors[1] = (1, 1)
for i in range(1, n+1):
    for j in range(sizes[i]):
        factors[outgoing[i][j]] = add(factors[outgoing[i][j]], mul((1, sizes[i]), factors[i]))

for i in range(1, len(factors)):
    if sizes[i]:
        factors[i] = mul(factors[i], (1, sizes[i]))

ans = sizes[1] if sizes[1] else 1

for i in range(1, len(factors)):
    if sizes[i] == 0 or factors[i][0] == 0:
        continue
    ans = lcm(ans, factors[i][1])

print(ans)