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
edges = []

def calcualteNWD(a, b):
    while(b>0):
        t=b
        b=a%b
        a=t

    return a

def calculateNWW(a, b):
    out = a//calcualteNWD(a,b)
    out*=b
    #print("NWW: "+str(a)+" "+str(b)+" "+str(out))
    return out

def updateValues(sv, n):
    edges[0]["incoming"].append([1,1])

    for i in range(0,n):
        #print(i)
        if len(edges[i]["neighbours"]) == 0:
            continue
        orsv = sv
        cv = 0
        for j in range(0,len(edges[i]["incoming"])):
            cv+=edges[i]["incoming"][j][0]*(sv//edges[i]["incoming"][j][1])
            if cv%1!=0: 
                return -1
            #print(j)
            #(edges[i].incoming[j].first * (sv / edges[i].incoming[j].second))
        if cv==0:
            continue
        if cv%len(edges[i]["neighbours"])!=0:
            sv *= (calculateNWW(cv, len(edges[i]["neighbours"]))//cv)
            if sv%1!=0: 
                return -1
            #print(sv)
        cv *= (sv//orsv)
        for j in range(0,len(edges[i]["neighbours"])):
            edges[edges[i]["neighbours"][j]-1]["incoming"].append([cv//len(edges[i]["neighbours"]), sv])
    return sv

x = input()
x=int(x)

for i in range(0,x):
    a=""
    a=input()
    a=a.split(" ")
    k = int(a[0])
    edges.append({"neighbours":[],"incoming":[]})
    for j in range(0,k):
        b = int(a[j+1])
        #print(edges[0]["neighbours"])
        edges[i]["neighbours"].append(b)
        #print(edges[i].neighbours[j])

#for i in range(0,x):
#    print(edges[i]["neighbours"])

print(int(updateValues(1,x)))