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
#!/usr/bin/env python3

from fractions import Fraction
import math


def main():
    N = int(input())
    conveyor = [[]]
    for i in range(N):
        row = [int(x) for x in input().split()]
        ri = row[0]
        conveyor.append([])
        for j in range(ri):
            conveyor[i + 1].append(row[j + 1])

    freq = [Fraction(0)] * (N + 1)
    freq[1] = Fraction(1)
    per = []
    for i in range(1, N + 1):
        if len(conveyor[i]) == 0 or freq[i] == 0:
            continue
        per.append(Fraction(len(conveyor[i]), freq[i]).numerator)
        for j in conveyor[i]:
            freq[j] += freq[i] * Fraction(1, len(conveyor[i]))
    print(math.lcm(*per))


if __name__ == "__main__":
    main()