N = int(eval(input()))

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



cnt, i = 0, 0

for j in range(10 ** 6):

    cnt += 1

    if a[i] == 2:

        print(cnt)

        #break

        quit()

    else:

        i = a[i] - 1

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

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



cnt, i = 0, 0

for j in range(len(a)):

    cnt += 1

    if a[i] == 2:

        print(cnt)

        #break

        quit()

    else:

        i = a[i] - 1

print((-1))
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
cnt, i = 0, 0
for j in range(10**6):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        # break
        quit()
    else:
        i = a[i] - 1
print((-1))

N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
cnt, i = 0, 0
for j in range(len(a)):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        # break
        quit()
    else:
        i = a[i] - 1
print((-1))

-for j in range(10**6): +for j in range(len(a)):