import sys

input = sys.stdin.readline



n = int(eval(input()))

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

light = 0

count = 0

while light != 1:

    if buttons[light] == -1:

        print((-1))

        break

    num = light

    light = buttons[num]

    buttons[num] = -1

    count += 1

else:

    print(count)
import sys

input = sys.stdin.readline



n = int(eval(input()))

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

count,light = 1,buttons[0]

while light != 2 and count<n:

    count,light = count+1,buttons[light-1]

print((count if count<n else -1))
import sys

input = sys.stdin.readline
n = int(eval(input()))
buttons = [int(eval(input())) - 1 for n in range(n)]
light = 0
count = 0
while light != 1:
    if buttons[light] == -1:
        print((-1))
        break
    num = light
    light = buttons[num]
    buttons[num] = -1
    count += 1
else:
    print(count)

import sys

input = sys.stdin.readline
n = int(eval(input()))
buttons = [int(eval(input())) for n in range(n)]
count, light = 1, buttons[0]
while light != 2 and count < n:
    count, light = count + 1, buttons[light - 1]
print((count if count < n else -1))

-buttons = [int(eval(input())) - 1 for n in range(n)] -light = 0 -count = 0 -while light != 1: -    if buttons[light] == -1: -        print((-1)) -        break -    num = light -    light = buttons[num] -    buttons[num] = -1 -    count += 1 -else: -    print(count) +buttons = [int(eval(input())) for n in range(n)] +count, light = 1, buttons[0] +while light != 2 and count < n: +    count, light = count + 1, buttons[light - 1] +print((count if count < n else -1))