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
from math import *

n = int(input())
out = [[] for i in range(n+1)]
for i in range(1, n+1):
	tmp = input().split()
	k = int(tmp[0])
	if len(tmp) != k + 1:
		print("wrong count")
		exit(1)
	for j in range(1, k+1):
		out[i].append(int(tmp[j]))

shifts = [0] * (n + 1)
shifts[1] = 1

for i in range(1, n+1):
	a = shifts[i]
	b = len(out[i])
	if b != 0 and a % b != 0:
		g = gcd(a, b)
		mul = b // g
		for j in range(1, n+1):
			shifts[j] *= mul
	for j in out[i]:
		shifts[j] += shifts[i] // len(out[i])
print(shifts[1])