# ABC065 B - Trained?

N = int(eval(input()))

A = [[int(eval(input())),False] for i in range(N)]

idx = 0

A[0][1] == True

cnt = 1



while cnt != N+1:

    if A[idx][1]:

        print((-1))

        exit()

    if A[idx][0] == 2:

        print(cnt)

        exit()

    A[idx][1] = True

    idx = A[idx][0]-1

    cnt += 1

n=int(eval(input()))

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

btn=[0]*n

ans=1

i=0

while True:

  if btn[i]==1:

    print((-1))

    exit()

  if a[i]==2:

    print(ans)

    exit()

  btn[i]=1

  i=a[i]-1

  ans+=1

# ABC065 B - Trained?
N = int(eval(input()))
A = [[int(eval(input())), False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1
while cnt != N + 1:
    if A[idx][1]:
        print((-1))
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0] - 1
    cnt += 1

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
btn = [0] * n
ans = 1
i = 0
while True:
    if btn[i] == 1:
        print((-1))
        exit()
    if a[i] == 2:
        print(ans)
        exit()
    btn[i] = 1
    i = a[i] - 1
    ans += 1

-# ABC065 B - Trained? -N = int(eval(input())) -A = [[int(eval(input())), False] for i in range(N)] -idx = 0 -A[0][1] == True -cnt = 1 -while cnt != N + 1: -    if A[idx][1]: +n = int(eval(input())) +a = [int(eval(input())) for i in range(n)] +btn = [0] * n +ans = 1 +i = 0 +while True: +    if btn[i] == 1: -    if A[idx][0] == 2: -        print(cnt) +    if a[i] == 2: +        print(ans) -    A[idx][1] = True -    idx = A[idx][0] - 1 -    cnt += 1 +    btn[i] = 1 +    i = a[i] - 1 +    ans += 1