N = int(eval(input()))

A = [0]

for _ in range(N):

    A.append(int(eval(input())))

S = set()

i = 1

cnt = 0

while i != 2 and not i in S:

    S.add(i)

    i = A[i]

    cnt += 1

if i == 2:

    print(cnt)

else:

    print((-1))
import sys

N = int(eval(input()))

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

x = 0

cnt = 0

for _ in range(N):

    x = A[x] - 1

    cnt += 1

    if x == 1:

        print(cnt)

        sys.exit()

print((-1))
N = int(eval(input()))
A = [0]
for _ in range(N):
    A.append(int(eval(input())))
S = set()
i = 1
cnt = 0
while i != 2 and not i in S:
    S.add(i)
    i = A[i]
    cnt += 1
if i == 2:
    print(cnt)
else:
    print((-1))

import sys

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
x = 0
cnt = 0
for _ in range(N):
    x = A[x] - 1
    cnt += 1
    if x == 1:
        print(cnt)
        sys.exit()
print((-1))

+import sys + -A = [0] +A = [int(eval(input())) for _ in range(N)] +x = 0 +cnt = 0 -    A.append(int(eval(input()))) -S = set() -i = 1 -cnt = 0 -while i != 2 and not i in S: -    S.add(i) -    i = A[i] +    x = A[x] - 1 -if i == 2: -    print(cnt) -else: -    print((-1)) +    if x == 1: +        print(cnt) +        sys.exit() +print((-1))