N = int(eval(input()))



a = []

for _ in range(N):

    a += [int(eval(input()))]



lst = [0] * N



flag = True

i = 0

count = 0

while flag:

    if lst[i] == 1:

        print((-1))

        exit()

    elif a[i] == 2:

        print((count + 1))

        exit()

    else:

        lst[i] = 1

        i = a[i] - 1

        count += 1

import sys

input = sys.stdin.readline



N = int(eval(input()))

A = [int(eval(input())) for _ in range(N)]



check = [True] * (N + 1)



count = 0

tmp = 1

while check[tmp]:

    count += 1

    check[tmp] = False

    if A[tmp - 1] == 2:

        print (count)

        exit()

    tmp = A[tmp - 1]



print((-1))
N = int(eval(input()))
a = []
for _ in range(N):
    a += [int(eval(input()))]
lst = [0] * N
flag = True
i = 0
count = 0
while flag:
    if lst[i] == 1:
        print((-1))
        exit()
    elif a[i] == 2:
        print((count + 1))
        exit()
    else:
        lst[i] = 1
        i = a[i] - 1
        count += 1

import sys

input = sys.stdin.readline
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
check = [True] * (N + 1)
count = 0
tmp = 1
while check[tmp]:
    count += 1
    check[tmp] = False
    if A[tmp - 1] == 2:
        print(count)
        exit()
    tmp = A[tmp - 1]
print((-1))

+import sys + +input = sys.stdin.readline -a = [] -for _ in range(N): -    a += [int(eval(input()))] -lst = [0] * N -flag = True -i = 0 +A = [int(eval(input())) for _ in range(N)] +check = [True] * (N + 1) -while flag: -    if lst[i] == 1: -        print((-1)) +tmp = 1 +while check[tmp]: +    count += 1 +    check[tmp] = False +    if A[tmp - 1] == 2: +        print(count) -    elif a[i] == 2: -        print((count + 1)) -        exit() -    else: -        lst[i] = 1 -        i = a[i] - 1 -        count += 1 +    tmp = A[tmp - 1] +print((-1))