1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import math

n = int(input())
r = [[int(x) - 1 for x in input().split()[1:]] for _ in range(n)]
q = [[] for _ in range(n)]
for i, x in enumerate(r):
	for j in x:
		q[j] += [i]
c0 = max(len(r[0]), 1)
a, c = [c0], [c0]
for i in range(1, len(r)):
	c1 = c[-1]
	a1 = sum(c1 // c[j] * a[j] // len(r[j]) for j in q[i])
	if not a1:
		a += [0]
		c += [c1]
		continue
	a2 = math.lcm(a1, max(len(r[i]), 1))
	c2 = a2 // a1 * c1
	a += [a2]
	c += [c2]
print(c[-1])