import sys



n = int(eval(input()))

a = []

for i in range(n):

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



cnt = 0

t = 1

for i in a:

    t = a[t-1]

    cnt += 1

    if t == 2:

        print(cnt)

        sys.exit()

print("-1")

N = int(eval(input()))

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

cnt = 0

tmp = 1

for i in A:

    tmp = A[tmp-1]

    cnt += 1

    if tmp == 2:

        print(cnt)

        exit()

print("-1")

import sys

n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))
cnt = 0
t = 1
for i in a:
    t = a[t - 1]
    cnt += 1
    if t == 2:
        print(cnt)
        sys.exit()
print("-1")

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
cnt = 0
tmp = 1
for i in A:
    tmp = A[tmp - 1]
    cnt += 1
    if tmp == 2:
        print(cnt)
        exit()
print("-1")

-import sys - -n = int(eval(input())) -a = [] -for i in range(n): -    a.append(int(eval(input()))) +N = int(eval(input())) +A = [int(eval(input())) for _ in range(N)] -t = 1 -for i in a: -    t = a[t - 1] +tmp = 1 +for i in A: +    tmp = A[tmp - 1] -    if t == 2: +    if tmp == 2: -        sys.exit() +        exit()