#coding: utf-8



N = int(eval(input()))

a = []

for i in range(N):

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



start = 0

count = 0



for i in range(N):

    count += 1

    next = a[start]

    if(next == 2):

        print(count)

        break

    start = next - 1

else:

    print((-1))
import sys



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

bot = N[1]



for i in range(N[0]):

    if bot == 2:

        print((i+1))

        break

    bot = N[bot]

else:

    print((-1))
# coding: utf-8
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))
start = 0
count = 0
for i in range(N):
    count += 1
    next = a[start]
    if next == 2:
        print(count)
        break
    start = next - 1
else:
    print((-1))

import sys

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

-# coding: utf-8 -N = int(eval(input())) -a = [] -for i in range(N): -    a.append(int(eval(input()))) -start = 0 -count = 0 -for i in range(N): -    count += 1 -    next = a[start] -    if next == 2: -        print(count) +import sys + +N = list(map(int, sys.stdin)) +bot = N[1] +for i in range(N[0]): +    if bot == 2: +        print((i + 1)) -    start = next - 1 +    bot = N[bot]