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
from collections import defaultdict
V = defaultdict(list)

n = int(input())

cnt = [0] * n
idx = [0] * n

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 chuj(pakuje_walize):
    for i in range(1,n):
        cnt[i] = 0
    cnt[0] = pakuje_walize ### asie basie i elize
    for i in range(n):
        if cnt[i] == 0 or len(V[i]) == 0:
            continue
        ile = len(V[i])
        essa = 0
        for j in range(idx[i], ile):
            essa += 1
            val = cnt[i]//ile
            if essa <= cnt[i]%ile:
                val += 1
            cnt[V[i][j]-1] += val
        for j in range(idx[i]):
            essa += 1
            val = cnt[i]//ile
            if essa <= cnt[i]%ile:
                val += 1
            cnt[V[i][j]-1] += val
        idx[i] = (idx[i] + cnt[i])%ile
    
    

for i in range(n):
    a = list(map(int, input().split()))
    for j in range(a[0]):
        V[i].append(a[j+1])
'''
for i in range(n):
    print(V[i])
'''
ans = len(V[0])
chuj(ans)

for i in range(1,n):
    if idx[i] == 0:
        continue
    midas = (lcm(len(V[i]), idx[i]) // idx[i]) * ans
    chuj(midas - ans)
    ans = midas

if ans == 0:
    ans = 69
    ans -= 68
print(ans)

'''

5
2 2 3
2 3 4
2 4 5
1 5
0

'''