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

import numbers
import fractions
import sys
import math

def debug(*args, **kwargs):
    pass
    print(*args, file=sys.stderr, **kwargs)

n = int(sys.stdin.readline().strip())
edges = []
incoming_edges = [[] for _ in range(n)]
total_num = 1

# Ile walizek musimy (średnio do resetu) dostarczyć
# do pierwszej platformy, by zrobić reset tej platformy.
counts_in_first = [fractions.Fraction(0, 1) for _ in range(n)]
counts_in_first[0] = fractions.Fraction(1, 1)
for i in range(n):
    r, *rest = map(int, sys.stdin.readline().strip().split(" "))
    elist = list(map(lambda x: x-1, rest))
    assert r == len(elist), f"{r} should be the length of edges {elist} for {i}"
    for e in elist:
        incoming_edges[e].append(i)
        counts_in_first[e] += counts_in_first[i] / len(elist)
    #debug(f"#0 needed for one in {i} {counts_in_first[i]}")
    edges.append(elist)
    # 
    if len(elist) != 0 and counts_in_first[i] != 0:
        counts_in_first[i] /= len(elist)
        #debug(f"It has {len(elist)} outgoing")
        total_num = math.lcm(total_num, counts_in_first[i].denominator)

#debug(f"{counts_in_first}")
#debug(f"{edges}")
print(total_num)