N=int(eval(input()))

D={i+1:int(eval(input())) for i in range(N)}

d=D[1]

ans=1

for i in range(N):

    if d==2:

        print(ans)

        exit()

    d=D[d]

    ans+=1

print((-1))
N=int(eval(input()))

A={i:int(eval(input())) for i in range(1,N+1)}



flag=0

k=1

for i in range(N):

    k=A[k]

    if k==2: flag=1; break



if flag: print((i+1))

else: print((-1))
N = int(eval(input()))
D = {i + 1: int(eval(input())) for i in range(N)}
d = D[1]
ans = 1
for i in range(N):
    if d == 2:
        print(ans)
        exit()
    d = D[d]
    ans += 1
print((-1))

N = int(eval(input()))
A = {i: int(eval(input())) for i in range(1, N + 1)}
flag = 0
k = 1
for i in range(N):
    k = A[k]
    if k == 2:
        flag = 1
        break
if flag:
    print((i + 1))
else:
    print((-1))

-D = {i + 1: int(eval(input())) for i in range(N)} -d = D[1] -ans = 1 +A = {i: int(eval(input())) for i in range(1, N + 1)} +flag = 0 +k = 1 -    if d == 2: -        print(ans) -        exit() -    d = D[d] -    ans += 1 -print((-1)) +    k = A[k] +    if k == 2: +        flag = 1 +        break +if flag: +    print((i + 1)) +else: +    print((-1))