n=int(eval(input()))

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

now=0

x=[0]*n

i=0

while x[now]==0:

    i+=1

    x[now]=1

    now=a[now]

    if now==1:

        print(i)

        break

else:

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

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

anow = 1

count = 0

for i in range(N):

    anow = a[anow-1]

    count += 1

    if anow == 2:

        break

print((count if anow==2 else '-1'))
n = int(eval(input()))
a = [int(eval(input())) - 1 for i in range(n)]
now = 0
x = [0] * n
i = 0
while x[now] == 0:
    i += 1
    x[now] = 1
    now = a[now]
    if now == 1:
        print(i)
        break
else:
    print((-1))

N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
anow = 1
count = 0
for i in range(N):
    anow = a[anow - 1]
    count += 1
    if anow == 2:
        break
print((count if anow == 2 else "-1"))

-n = int(eval(input())) -a = [int(eval(input())) - 1 for i in range(n)] -now = 0 -x = [0] * n -i = 0 -while x[now] == 0: -    i += 1 -    x[now] = 1 -    now = a[now] -    if now == 1: -        print(i) +N = int(eval(input())) +a = [int(eval(input())) for i in range(N)] +anow = 1 +count = 0 +for i in range(N): +    anow = a[anow - 1] +    count += 1 +    if anow == 2: -else: -    print((-1)) +print((count if anow == 2 else "-1"))