n = int(eval(input()))

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

chk = [False for i in range(n)]



c = 0

d = 0

while True:

	if chk[c]:

		print((-1))

		exit()

	else:

		chk[c] = True

		if c == 1:

			print (d)

			exit()

	c = a[c]-1

	d += 1
n = int(eval(input()))

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



c = a[0]

for i in range(n):

	if c == 2:

		print((i+1))

		exit()

	c = a[c-1]

print((-1))
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
chk = [False for i in range(n)]
c = 0
d = 0
while True:
    if chk[c]:
        print((-1))
        exit()
    else:
        chk[c] = True
        if c == 1:
            print(d)
            exit()
    c = a[c] - 1
    d += 1

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
c = a[0]
for i in range(n):
    if c == 2:
        print((i + 1))
        exit()
    c = a[c - 1]
print((-1))

-chk = [False for i in range(n)] -c = 0 -d = 0 -while True: -    if chk[c]: -        print((-1)) +c = a[0] +for i in range(n): +    if c == 2: +        print((i + 1)) -    else: -        chk[c] = True -        if c == 1: -            print(d) -            exit() -    c = a[c] - 1 -    d += 1 +    c = a[c - 1] +print((-1))