import sys

nums = int(eval(input()))

bottom = list(range(nums))

check = {}

count = 0

a = 0

for i in range(nums):

    bottom[i] = int(eval(input()))

    check[i] = 0



while True:



    if bottom[a] == 2:

        count += 1

        break

    if check[a] == 1:

        print("-1")

        sys.exit()

    check[a] = 1

    a = bottom[a] - 1

    count += 1

print(count)
a = [int(eval(input())) for _ in range(int(eval(input())))]

c,s = 1,a[0]

for i in range(len(a)):

    if s ==2:

        print(c)

        break

    c,s = c+1, a[s-1]

else:print((-1))
import sys

nums = int(eval(input()))
bottom = list(range(nums))
check = {}
count = 0
a = 0
for i in range(nums):
    bottom[i] = int(eval(input()))
    check[i] = 0
while True:
    if bottom[a] == 2:
        count += 1
        break
    if check[a] == 1:
        print("-1")
        sys.exit()
    check[a] = 1
    a = bottom[a] - 1
    count += 1
print(count)

a = [int(eval(input())) for _ in range(int(eval(input())))]
c, s = 1, a[0]
for i in range(len(a)):
    if s == 2:
        print(c)
        break
    c, s = c + 1, a[s - 1]
else:
    print((-1))

-import sys - -nums = int(eval(input())) -bottom = list(range(nums)) -check = {} -count = 0 -a = 0 -for i in range(nums): -    bottom[i] = int(eval(input())) -    check[i] = 0 -while True: -    if bottom[a] == 2: -        count += 1 +a = [int(eval(input())) for _ in range(int(eval(input())))] +c, s = 1, a[0] +for i in range(len(a)): +    if s == 2: +        print(c) -    if check[a] == 1: -        print("-1") -        sys.exit() -    check[a] = 1 -    a = bottom[a] - 1 -    count += 1 -print(count) +    c, s = c + 1, a[s - 1] +else: +    print((-1))