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
import copy

def gcd(x, y):
    while(y):
       x, y = y, x % y
    return x

n = int(input())

graph = []
rev = []
for i in range(n):
    rev.append([])

for u in range(n):
    neis = list(map(int,input().split()))[1:]
    for i in range(len(neis)):
        neis[i] -= 1
    graph.append(neis)
    for v in neis:
        rev[v].append(u)

#print(graph)

dp = []
for i in range(n):  
    dp.append(0)

dp[0] = 1

for u in range(n):
    for v in rev[u]:
        dp[u] += int(int(dp[v]) / int(len(graph[v])))

    if dp[u] == 0: continue

    deg = int(max(1, len(graph[u])))
    if int(dp[u]) % int(deg) != 0:
        #assert int(deg) % int(gcd(dp[u], deg)) == 0
        mult = int(int(deg) / int(gcd(dp[u], deg)))

        for i in range(u + 1):
            dp[i] = int(int(dp[i]) * int(mult))
    
#print(dp)
print(int(dp[0]))