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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
NAX = 110
n = 0

edges_f = [[] for _ in range(NAX)]
edges_b = [[] for _ in range(NAX)]
cycle_counts = [0] * NAX

def read_input():
    global n
    n = int(input())
    for i in range(1, n+1):
        x = input().split(" ")
        for l in x[1:]:
            l = int(l)
            edges_f[i].append(l)
            edges_b[l].append(i)

res = 1

def update(idx: int):
    global res
    global cycle_counts

    if idx == 1:
        cycle_counts[1] = 1
        res = max(1, len(edges_f[1]))
        return
    if len(edges_b[idx]) == 0 or len(edges_f[idx]) == 0:
        return # co jeśli to da true dla idx == 1?

    count = 0
    for i in edges_b[idx]:
        count += cycle_counts[i]

    edges_f_len = len(edges_f[idx])

    # print(f"{count=}")

    where = count
    li = 1
    while where % edges_f_len != 0:
        where += count
        li += 1

    cycle_counts[idx] = where // edges_f_len
    for i in range(1, idx):
        cycle_counts[i] *= li
    res *= li

def main() -> None:
    read_input()
    for i in range(1, n+1):
        update(i)
        # print(f"after {i}")
        # print(cycle_counts[1:n+1])
    print(res)


if __name__ == "__main__":
    main()

"""
7
3 2 3 5
2 3 6
3 5 6 7
1 6
1 7
0
0
* 6

12
4 3 8 9 12
6 4 7 8 9 10 11
4 4 7 8 9
4 6 7 8 12
2 7 12
3 8 9 11
3 8 10 11
3 9 10 11
1 12
1 12
0
0
* 192

5
2 2 3
2 3 4
2 4 5
1 5
0
* 8
"""