n=int(eval(input()))

a=[int(eval(input())) for i in range(n)]

c,s=1,a[0]

while s!=2 and c<n:c,s=c+1,a[s-1]

print((c if c<n else -1))
N=int(eval(input()))

a=[int(eval(input())) for i in range(N)]

count=1

button=a[0]

while button!=2 and count<N:

    count+=1

    button=a[button-1]

print((count if count<N else -1))

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
c, s = 1, a[0]
while s != 2 and c < n:
    c, s = c + 1, a[s - 1]
print((c if c < n else -1))

N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
count = 1
button = a[0]
while button != 2 and count < N:
    count += 1
    button = a[button - 1]
print((count if count < N else -1))

-n = int(eval(input())) -a = [int(eval(input())) for i in range(n)] -c, s = 1, a[0] -while s != 2 and c < n: -    c, s = c + 1, a[s - 1] -print((c if c < n else -1)) +N = int(eval(input())) +a = [int(eval(input())) for i in range(N)] +count = 1 +button = a[0] +while button != 2 and count < N: +    count += 1 +    button = a[button - 1] +print((count if count < N else -1))