N = int(eval(input()))

A = []

ex = [0]*N

light = 0

c = 0

for i in range(N) :

    A.append(int(eval(input())))



while light != 1 :

    if ex[light] == 1 :

        print('-1')

        exit()

    ex[light] = 1

    light = A[light]-1

    c += 1

    



print(c)
N = int(eval(input()))

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

A = list([x-1 for x in A])

ex = [0]*10**5

light = 0

for i in range(N) :

    if light == 1 :

        print(i)

        exit()

    elif ex[light] == 1 :

        print('-1')

        exit()

    ex[light] = 1

    light = A[light]



print('-1')
N = int(eval(input()))
A = []
ex = [0] * N
light = 0
c = 0
for i in range(N):
    A.append(int(eval(input())))
while light != 1:
    if ex[light] == 1:
        print("-1")
        exit()
    ex[light] = 1
    light = A[light] - 1
    c += 1
print(c)

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
A = list([x - 1 for x in A])
ex = [0] * 10**5
light = 0
for i in range(N):
    if light == 1:
        print(i)
        exit()
    elif ex[light] == 1:
        print("-1")
        exit()
    ex[light] = 1
    light = A[light]
print("-1")

-A = [] -ex = [0] * N +A = [int(eval(input())) for _ in range(N)] +A = list([x - 1 for x in A]) +ex = [0] * 10**5 -c = 0 -    A.append(int(eval(input()))) -while light != 1: -    if ex[light] == 1: +    if light == 1: +        print(i) +        exit() +    elif ex[light] == 1: -    light = A[light] - 1 -    c += 1 -print(c) +    light = A[light] +print("-1")