N = int(eval(input()))

a = []

for i in range(N):

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



pushed = [0] * N

pushed[0] = 1



current_bottom = a[0]

ans = 1

if current_bottom == 2:

    print(ans)

    exit()



for _ in range(N):

    current_bottom = a[current_bottom-1]

    if pushed[current_bottom-1] == 1:

        print((-1))

        exit()

    else:

        pushed[current_bottom-1] = 1

    ans += 1

    if current_bottom == 2:

        print(ans)

        exit()
N = int(eval(input()))

a = []

for i in range(N):

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



current_bottom = a[0]

ans = 1

if current_bottom == 2:

    print(ans)

    exit()



for _ in range(N):

    current_bottom = a[current_bottom-1]

    ans += 1

    if current_bottom == 2:

        print(ans)

        exit()

print((-1))
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))
pushed = [0] * N
pushed[0] = 1
current_bottom = a[0]
ans = 1
if current_bottom == 2:
    print(ans)
    exit()
for _ in range(N):
    current_bottom = a[current_bottom - 1]
    if pushed[current_bottom - 1] == 1:
        print((-1))
        exit()
    else:
        pushed[current_bottom - 1] = 1
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()

N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))
current_bottom = a[0]
ans = 1
if current_bottom == 2:
    print(ans)
    exit()
for _ in range(N):
    current_bottom = a[current_bottom - 1]
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()
print((-1))

-pushed = [0] * N -pushed[0] = 1 -    if pushed[current_bottom - 1] == 1: -        print((-1)) -        exit() -    else: -        pushed[current_bottom - 1] = 1 +print((-1))