n = int(eval(input()))

A = [int(eval(input())) for i in range(n)]

B = []

for i, a in enumerate(A):

    B.append([i + 1, a])

    

idx = 1

for i in range(n):

    x = B[idx - 1][1]

    if x == 2:

        print((i + 1))

        break

    else:

        idx = x

else:

    print((-1))
n = int(eval(input()))

A = [int(eval(input())) for i in range(n)]



cnt = 1

x = A[0]

while x != 2 and cnt != n:

    x = A[x - 1]

    cnt += 1

    

if cnt == n:

    print((-1))

else:

    print(cnt)
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
B = []
for i, a in enumerate(A):
    B.append([i + 1, a])
idx = 1
for i in range(n):
    x = B[idx - 1][1]
    if x == 2:
        print((i + 1))
        break
    else:
        idx = x
else:
    print((-1))

n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
cnt = 1
x = A[0]
while x != 2 and cnt != n:
    x = A[x - 1]
    cnt += 1
if cnt == n:
    print((-1))
else:
    print(cnt)

-B = [] -for i, a in enumerate(A): -    B.append([i + 1, a]) -idx = 1 -for i in range(n): -    x = B[idx - 1][1] -    if x == 2: -        print((i + 1)) -        break -    else: -        idx = x +cnt = 1 +x = A[0] +while x != 2 and cnt != n: +    x = A[x - 1] +    cnt += 1 +if cnt == n: +    print((-1)) -    print((-1)) +    print(cnt)