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
# Python 4 lyf
from fractions import Fraction
import math


n = int(input())
inadj = []
outdeg = []
# X = 1
for i in range(n):
    inadj.append([])
for i in range(n):
    arr = input().split()
    ile = int(arr[0])
    outdeg.append(ile)
    # if ile: # broken with 0.
    #     X = math.lcm(X, ile)
    # ^^ was in wrong place bcs included unreachables.
    for x in range(ile):
        inadj[int(arr[x + 1]) - 1].append(i)
dp = [Fraction(1, 1)]
res = 1
X = outdeg[0]
for i in range(1, n):
    cr = Fraction(0, 1)
    for x in inadj[i]:
        cr += dp[x] / Fraction(outdeg[x], 1)
    dp.append(cr)
    # Actual fix:

    # if outdeg[i] > 0: # sure > 1 tez could be.
    #     # res = math.lcm(res, (cr * Fraction(X, outdeg[i])).denominator)
    #     # fix:
    #     if cr:
    #         X = math.lcm(X, outdeg[i])

    # Zakomentowanie ^^ i just having X == outdeg[0] and ; and nuitka3-run
    # wal.py and alt+;+; and enter -> all tests passed
for i in range(1, n):
    cr = dp[i]
    if cr and outdeg[i] > 0:
        res = math.lcm(res, (cr * Fraction(X, outdeg[i])).denominator)
# print(X)
# print(dp)
print(max(1, res * X))