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
46
47
48
49
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    return (a * b) // gcd(a, b)


def lcm_of_list(xs):
    result = xs[0]
    for x in xs:
        result = lcm(result, x)
    return result


def solve(N, in_edges, out_edges):
    dp = [(len(out_edges[0]), 1)]
    T = len(out_edges[0])
    for i, prev in enumerate(in_edges):
        if i == 0:
            continue
        received = 0
        for j in prev:
            if dp[j][0]:
                received += (T // dp[j][0]) * dp[j][1]

        if received == 0 or len(out_edges[i]) == 0:
            dp.append((T, 0))
            continue

        required = lcm(received, len(out_edges[i]))
        resets = required // len(out_edges[i])
        T *= required // received
        dp.append((T, resets))

    return T or 1


N = int(input())
out_edges = [[int(i) - 1 for i in input().split(' ')[1:] if i]
             for _ in range(N)]

in_edges = [[] for _ in range(N)]
for i, es in enumerate(out_edges):
    for e in es:
        in_edges[e].append(i)
print(solve(N, in_edges, out_edges))