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
import math
from fractions import Fraction
from typing import List


def solve(n: int, links: List[List[int]]) -> int:
    links_rev = [[] for _ in range(n)]
    for i in range(n):
        for j in links[i]:
            links_rev[j].append(i)

    fracs = []
    for i in range(n):
        frac = Fraction(int(i == 0), 1)

        for j in links_rev[i]:
            frac += fracs[j]

        if links[i]:
            frac /= len(links[i])

        fracs.append(frac)

    lcm = 1
    for frac in fracs:
        lcm = math.lcm(lcm, frac.denominator)

    return lcm

def main():
    n = int(input())
    links = [
        [int(id) - 1 for id in input().split(" ")[1:]]
        for _ in range(n)
    ]

    print(solve(n, links))


main()