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

def lcm(a, b) :
	if a == 0 or b == 0 :
		return a + b
	return (a//gcd(a,b)) * b;

n = int(input())

gin = [[] for i in range(n)]
gout = [[] for i in range(n)]
os = [False for i in range(n)]

ans = 1
xd = [[0,0] for i in range(n)]

for i in range(n) :
	lis =  input().split()
	for j in range(1, int(lis[0])+1) :
		u = int(lis[j]) 
		u -= 1;
		gout[i].append(u)
		gin[u].append(i);

os[0] = True

for i in range(1,n) :
	for j in gin[i] :
		if os[j] :
			os[i] = True

xd[0] = [len(gout[0]),1];
for i in range(1,n) :
	if not os[i]:
		continue;
	
	hehe = []
	for v in gin[i] :
		if not os[v]:
			continue;
		hehe.append(xd[v])

	nn = 1;
	for j in range(len(hehe)):
		nn = lcm(nn, hehe[j][0]);
	
	ile = 0;
	for j in range(len(hehe)):
		ile += (nn//hehe[j][0]) * hehe[j][1];

	k = len(gout[i])
	if k != 0 :
		jeszcze = lcm(ile, k);
		nn = nn * (jeszcze//ile);
		xd[i] = [nn, jeszcze//k];

for i in range(n) :
	if (not os[i]) or  (len(gout[i]) == 0):
		continue;
	ans = lcm(ans, xd[i][0]);

print(ans)