import sys

input = sys.stdin.readline



buttons = list(map(int,sys.stdin))

n = buttons[0]

count,light = 1,buttons[1]

while light!=2 and count<n:

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

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

input = sys.stdin.readline



buttons = list(map(int,sys.stdin))

light = buttons[1]

for i in range(buttons[0]):

    if light==2:

        print((i+1))

        break

    light= buttons[light]

else:

    print((-1))
import sys

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

import sys

input = sys.stdin.readline
buttons = list(map(int, sys.stdin))
light = buttons[1]
for i in range(buttons[0]):
    if light == 2:
        print((i + 1))
        break
    light = buttons[light]
else:
    print((-1))

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