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
import sys

def inp():
    return sys.stdin.readline()

n = int(inp())

graph = []
for i in range(n):
    data = [int(x) - 1 for x in inp().split(" ")]
    data.pop(0)
    graph.append(data)

g_idx = [0] * n

changes = 0
while True:
    curr = 0
    while len(graph[curr]) > 0:
        prev_curr = curr
        curr = graph[curr][g_idx[curr]]
        g_idx[prev_curr] = (g_idx[prev_curr] + 1) % len(graph[prev_curr])

    changes += 1
    if max(g_idx) == 0:
        print(changes)
        break