n = int(eval(input()))

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

pushed = [False] * n

q = [0]

ans = 0

while q:

    b = q.pop()

    ans += 1

    pushed[b] = True

    if pushed[A[b]]:

        ans = -1

        break

    if A[b] == 1:

        break

    q.append(A[b])

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

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

pushed = [False] * n

b = 0

ans = 0

while True:

    ans += 1

    pushed[b] = True

    b = buttons[b] - 1

    if pushed[b]:

        ans = -1

        break

    if b == 1:

        break

        

print(ans)

n = int(eval(input()))
A = [int(eval(input())) - 1 for _ in range(n)]
pushed = [False] * n
q = [0]
ans = 0
while q:
    b = q.pop()
    ans += 1
    pushed[b] = True
    if pushed[A[b]]:
        ans = -1
        break
    if A[b] == 1:
        break
    q.append(A[b])
print(ans)

n = int(eval(input()))
buttons = [int(eval(input())) for _ in range(n)]
pushed = [False] * n
b = 0
ans = 0
while True:
    ans += 1
    pushed[b] = True
    b = buttons[b] - 1
    if pushed[b]:
        ans = -1
        break
    if b == 1:
        break
print(ans)

-A = [int(eval(input())) - 1 for _ in range(n)] +buttons = [int(eval(input())) for _ in range(n)] -q = [0] +b = 0 -while q: -    b = q.pop() +while True: -    if pushed[A[b]]: +    b = buttons[b] - 1 +    if pushed[b]: -    if A[b] == 1: +    if b == 1: -    q.append(A[b])