N = int(eval(input()))

a = {i: int(eval(input())) for i in range(1, N + 1)}

i = 1

cnt = 0

while True:

    cnt += 1

    if a[i] == 2:

        print(cnt)

        break

    if cnt == N:

        print((-1))

        break

    i = a[i]
n = int(eval(input()))

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

c = 1

s = a[0]

while s != 2 and c <= n:

    c = c + 1

    s = a[s-1]

print((c if c <= n else -1))
N = int(eval(input()))
a = {i: int(eval(input())) for i in range(1, N + 1)}
i = 1
cnt = 0
while True:
    cnt += 1
    if a[i] == 2:
        print(cnt)
        break
    if cnt == N:
        print((-1))
        break
    i = a[i]

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
c = 1
s = a[0]
while s != 2 and c <= n:
    c = c + 1
    s = a[s - 1]
print((c if c <= n else -1))

-N = int(eval(input())) -a = {i: int(eval(input())) for i in range(1, N + 1)} -i = 1 -cnt = 0 -while True: -    cnt += 1 -    if a[i] == 2: -        print(cnt) -        break -    if cnt == N: -        print((-1)) -        break -    i = a[i] +n = int(eval(input())) +a = [int(eval(input())) for i in range(n)] +c = 1 +s = a[0] +while s != 2 and c <= n: +    c = c + 1 +    s = a[s - 1] +print((c if c <= n else -1))