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
import math
n = input()
n = int(n)
lista1 = [[] for _ in range(n)]
lista2 = [[] for _ in range(n)]

for i in range(0, n):
    y = [int(x) for x in input().split()]
    a = y[0]
    for x in range(1, a+1):
        lista1[i].append(y[x]-1)
        lista2[y[x]-1].append(i)

res = [[0, 0] for _ in range(n)]
wyn = 1
res[0] = (int(len(lista1[0])), int(len(lista1[0])))

for i in range(0, n):
    if len(lista2[i]) == 0:
        continue
    nww = 1
    for a in lista2[i]:
        if res[a][0] != 0:
            nww = int(int(nww) * int(res[a][1]) // int(math.gcd(int(nww), int(res[a][1]))))
    res[i][1] = int(nww)
    for a in lista2[i]:
        if res[a][0] != 0:
            res[i][0] = int(res[i][0]) + (int(res[a][0]) * nww // int(len(lista1[a])) // int(res[a][1]))
    if res[i][0] == 0:
        continue
    if len(lista1[i]) > 0:
        temp = int(int(len(lista1[i])) * int(res[i][0]) // int(math.gcd(int(len(lista1[i])), int(res[i][0]))))
        res[i][1] = res[i][1] * (temp // res[i][0])
        res[i][0] = temp
for i in res:
    if i[1] != 0:
        wyn = int(int(wyn) * int(i[1]) // int(math.gcd(int(wyn), int(i[1]))))
print(wyn)