N=int(eval(input()))

L=[]

for i in range(N):

	L.append(int(eval(input())))

count=0

ans=0

i=0

while(1):

	count+=1

	if count==N+100:

		print((-1))

		exit()

	i=L[i]-1

	ans+=1

	if i==1:

		print(ans)

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

L=[]

count=0

t=0

for i in range(N):

	L.append(int(eval(input())))

while(1):

	count+=1

	if count>N+10:

		print((-1))

		exit()

	if L[t]==2:

		print(count)

		exit()

	else:

		t=L[t]-1
N = int(eval(input()))
L = []
for i in range(N):
    L.append(int(eval(input())))
count = 0
ans = 0
i = 0
while 1:
    count += 1
    if count == N + 100:
        print((-1))
        exit()
    i = L[i] - 1
    ans += 1
    if i == 1:
        print(ans)
        exit()

N = int(eval(input()))
L = []
count = 0
t = 0
for i in range(N):
    L.append(int(eval(input())))
while 1:
    count += 1
    if count > N + 10:
        print((-1))
        exit()
    if L[t] == 2:
        print(count)
        exit()
    else:
        t = L[t] - 1

+count = 0 +t = 0 -count = 0 -ans = 0 -i = 0 -    if count == N + 100: +    if count > N + 10: -    i = L[i] - 1 -    ans += 1 -    if i == 1: -        print(ans) +    if L[t] == 2: +        print(count) +    else: +        t = L[t] - 1