N = int(eval(input()))

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

a = [0] + a

count = 0

i = 1

while i != 2:

    j = i

    i = a[i]

    a[j] = -1

    count  += 1

    if i == -1:

        count = -1

        break

print(count)
n = int(eval(input()))

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

count = 0

i = 1

for _ in range(n):

    i = A[i - 1]

    count += 1

    if i == 2:

        print(count)

        exit()

print((-1))
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
a = [0] + a
count = 0
i = 1
while i != 2:
    j = i
    i = a[i]
    a[j] = -1
    count += 1
    if i == -1:
        count = -1
        break
print(count)

n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
count = 0
i = 1
for _ in range(n):
    i = A[i - 1]
    count += 1
    if i == 2:
        print(count)
        exit()
print((-1))

-N = int(eval(input())) -a = [int(eval(input())) for _ in range(N)] -a = [0] + a +n = int(eval(input())) +A = [int(eval(input())) for _ in range(n)] -while i != 2: -    j = i -    i = a[i] -    a[j] = -1 +for _ in range(n): +    i = A[i - 1] -    if i == -1: -        count = -1 -        break -print(count) +    if i == 2: +        print(count) +        exit() +print((-1))