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

n = int(input())
G = []
cnt = [0]*109

def f(x):
    for i in range(1, n+1):
        cnt[i] = 0
    cnt[1] = x
    for i in range(1, n+1):
        if(len(G[i])==0):
            continue;
        for y in G[i]:
            cnt[y] = int(cnt[y] + cnt[i]//len(G[i]))
        for j in range(0, int(cnt[i]%len(G[i]))):
            cnt[G[i][j]] = int(cnt[G[i][j]] + 1)

G.append([0])
for i in range(1,n+1):
    input_list = [ int(num) for num in input().split()]
    input_list.pop(0)
    G.append(input_list)
ans = 1
for i in range(1, n+1):
    f(ans)
    carry = int(cnt[i])
    if carry==0:
        continue
    if len(G[i])==0:
        continue
    ans = int(ans*len(G[i])//gcd(len(G[i]), carry))
print(ans)