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


n = int(input())
    
all_data = [{}]
inputs = [set()]
for i in range(1, n+1):
    inputs.append(set())
    
for i in range(1, n+1):
    all_data.append({int(x) for x in input().split(' ')[1:]})
    for x in all_data[i]:
        inputs[x] |= {i}

res = 1
amount_visits = [0]*(n+1)
for i in range(1, n+1):
    number_of_moves = 0
    outs = len(all_data[i])
    if i==1:
        number_of_moves = 1
        amount_visits[i] = outs
        
    if outs > 0:
        for x in inputs[i]:
            number_of_moves = (number_of_moves + amount_visits[x]//len(all_data[x]))%outs
        visit_multiplier = outs//gcd(number_of_moves, outs)
        res = res * visit_multiplier
        for x in range(1, i):
            amount_visits[x] *= visit_multiplier
    
    for x in inputs[i]:
        amount_visits[i] += amount_visits[x] // len(all_data[x])
print(res)