n = int(eval(input()))

a = []

for i in range(n):

    a.append(int(eval(input())) - 1)



# if not 1 in a:

#     print(-1)



# hst = []

btn = 0

cnt = 0



while cnt <= 10**7:

    # hst.append(btn)

    btn = a[btn]

    cnt += 1

    if btn == 1:

        print(cnt)

        exit()



print((-1))
n = int(eval(input()))

a = []

for i in range(n):

    a.append(int(eval(input())) - 1)



hst = [0] * n

btn = 0

cnt = 0



while hst[btn] != 1:

    hst[btn] = 1

    btn = a[btn]



    cnt += 1

    if btn == 1:

        print(cnt)

        exit()



print((-1))
n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())) - 1)
# if not 1 in a:
#     print(-1)
# hst = []
btn = 0
cnt = 0
while cnt <= 10**7:
    # hst.append(btn)
    btn = a[btn]
    cnt += 1
    if btn == 1:
        print(cnt)
        exit()
print((-1))

n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())) - 1)
hst = [0] * n
btn = 0
cnt = 0
while hst[btn] != 1:
    hst[btn] = 1
    btn = a[btn]
    cnt += 1
    if btn == 1:
        print(cnt)
        exit()
print((-1))

-# if not 1 in a: -#     print(-1) -# hst = [] +hst = [0] * n -while cnt <= 10**7: -    # hst.append(btn) +while hst[btn] != 1: +    hst[btn] = 1