n = int(eval(input()))

a = [int(eval(input())) for i in range(n)]

j = 0

for k in range(n):

    j = a[j] - 1

    if j == 1:

        print((k+1))

        break

    elif k == n-1:

        print((-1))

    else:

        pass
n = int(eval(input()))

a = [int(eval(input())) for i in range(n)]

j = 0

for k in range(n):

    j = a[j] - 1

    if j == 1:

        print((k+1))

        break

    else:

        pass

else:

    print((-1))
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
j = 0
for k in range(n):
    j = a[j] - 1
    if j == 1:
        print((k + 1))
        break
    elif k == n - 1:
        print((-1))
    else:
        pass

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
j = 0
for k in range(n):
    j = a[j] - 1
    if j == 1:
        print((k + 1))
        break
    else:
        pass
else:
    print((-1))

-    elif k == n - 1: -        print((-1)) +else: +    print((-1))