N = int(eval(input()))



a = []



for i in range(N):

    a.append(int(eval(input())) - 1)



done = [False] * N



i = 0



ans = 0



while True:

    if done[i] == False:

        done[i] = True

        ans += 1

        if a[i] == 1:

            break

        i = a[i]

    else:

        ans = -1

        break



print(ans)

import sys



l = [int(i) for i in sys.stdin]



done = [False] * (l[0] + 1)



i = 1



ans = 0



while True:

    if done[i] == False:

        done[i] = True

        ans += 1

        if l[i] == 2:

            break

        i = l[i]

    else:

        ans = -1

        break



print(ans)

N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())) - 1)
done = [False] * N
i = 0
ans = 0
while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break
print(ans)

import sys

l = [int(i) for i in sys.stdin]
done = [False] * (l[0] + 1)
i = 1
ans = 0
while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if l[i] == 2:
            break
        i = l[i]
    else:
        ans = -1
        break
print(ans)

-N = int(eval(input())) -a = [] -for i in range(N): -    a.append(int(eval(input())) - 1) -done = [False] * N -i = 0 +import sys + +l = [int(i) for i in sys.stdin] +done = [False] * (l[0] + 1) +i = 1 -        if a[i] == 1: +        if l[i] == 2: -        i = a[i] +        i = l[i]