n = int(eval(input()))



al = [0 for _ in range(n)]

for i in range(n):

    al[i] = int(eval(input()))



ans = 1

i = 0

while True:

    if al[i] == 2:

        print(ans)

        break

    elif al[i] == -1:

        print((-1))

        break

    tmp = i

    i = al[i]-1

    al[tmp] = -1

    ans += 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 = int(eval(input()))
al = [0 for _ in range(n)]
for i in range(n):
    al[i] = int(eval(input()))
ans = 1
i = 0
while True:
    if al[i] == 2:
        print(ans)
        break
    elif al[i] == -1:
        print((-1))
        break
    tmp = i
    i = al[i] - 1
    al[tmp] = -1
    ans += 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 = int(eval(input())) -al = [0 for _ in range(n)] -for i in range(n): -    al[i] = int(eval(input())) -ans = 1 -i = 0 -while True: -    if al[i] == 2: -        print(ans) +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)) -    elif al[i] == -1: -        print((-1)) -        break -    tmp = i -    i = al[i] - 1 -    al[tmp] = -1 -    ans += 1 +    light = buttons[light] +else: +    print((-1))