N = int(eval(input()))

array = []

for i in range(N):

    a = int(eval(input()))-1

    array.append(a)



c = 0

now = 0

for i in range(N):

    now = array[now]

    c += 1

    if now == 1:

        break

if c == N and now != 1:

    print((-1))

else:

    print(c)
N = int(eval(input()))

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

nexta = a[0]

ans = -1

for i in range(1,N):

    if nexta == 2:

        ans = i

        break

    nexta = a[nexta-1]

print(ans)
N = int(eval(input()))
array = []
for i in range(N):
    a = int(eval(input())) - 1
    array.append(a)
c = 0
now = 0
for i in range(N):
    now = array[now]
    c += 1
    if now == 1:
        break
if c == N and now != 1:
    print((-1))
else:
    print(c)

N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
nexta = a[0]
ans = -1
for i in range(1, N):
    if nexta == 2:
        ans = i
        break
    nexta = a[nexta - 1]
print(ans)

-array = [] -for i in range(N): -    a = int(eval(input())) - 1 -    array.append(a) -c = 0 -now = 0 -for i in range(N): -    now = array[now] -    c += 1 -    if now == 1: +a = [int(eval(input())) for i in range(N)] +nexta = a[0] +ans = -1 +for i in range(1, N): +    if nexta == 2: +        ans = i -if c == N and now != 1: -    print((-1)) -else: -    print(c) +    nexta = a[nexta - 1] +print(ans)