N = eval(input())

arr = [eval(input())-1 for i in range(N)]



now = 0

count = 0

while (now != 1):

    count += 1

    now = arr[now]

    if count > 100000:

        count = -1

        break

print(count)

N = int(eval(input()))

src = [int(eval(input())) for i in range(N)]

now = 0

for i in range(N):

    a = src[now] - 1

    if a == 1:

        print((i+1))

        exit()

    now = a

print((-1))
N = eval(input())
arr = [eval(input()) - 1 for i in range(N)]
now = 0
count = 0
while now != 1:
    count += 1
    now = arr[now]
    if count > 100000:
        count = -1
        break
print(count)

N = int(eval(input()))
src = [int(eval(input())) for i in range(N)]
now = 0
for i in range(N):
    a = src[now] - 1
    if a == 1:
        print((i + 1))
        exit()
    now = a
print((-1))

-N = eval(input()) -arr = [eval(input()) - 1 for i in range(N)] +N = int(eval(input())) +src = [int(eval(input())) for i in range(N)] -count = 0 -while now != 1: -    count += 1 -    now = arr[now] -    if count > 100000: -        count = -1 -        break -print(count) +for i in range(N): +    a = src[now] - 1 +    if a == 1: +        print((i + 1)) +        exit() +    now = a +print((-1))