N = int(eval(input()))

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

now = a[0]

count = 1

c = 0

for i in range(10**6):

    if now != 2:

        count += 1

        now = a[now - 1]

    else:

        print(count)

        c = 1

        break

if c == 0:

    print((-1))

N = int(eval(input()))

a = [0] + [int(eval(input())) for _ in range(N)]

x = 1

visited = set([1])

answer = 0



while True:

  x = a[x]

  if x in visited:

    answer = -1

    break

  visited.add(x)

  answer += 1

  if x == 2:

    break

    

print(answer)

N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
now = a[0]
count = 1
c = 0
for i in range(10**6):
    if now != 2:
        count += 1
        now = a[now - 1]
    else:
        print(count)
        c = 1
        break
if c == 0:
    print((-1))

N = int(eval(input()))
a = [0] + [int(eval(input())) for _ in range(N)]
x = 1
visited = set([1])
answer = 0
while True:
    x = a[x]
    if x in visited:
        answer = -1
        break
    visited.add(x)
    answer += 1
    if x == 2:
        break
print(answer)

-a = [int(eval(input())) for i in range(N)] -now = a[0] -count = 1 -c = 0 -for i in range(10**6): -    if now != 2: -        count += 1 -        now = a[now - 1] -    else: -        print(count) -        c = 1 +a = [0] + [int(eval(input())) for _ in range(N)] +x = 1 +visited = set([1]) +answer = 0 +while True: +    x = a[x] +    if x in visited: +        answer = -1 -if c == 0: -    print((-1)) +    visited.add(x) +    answer += 1 +    if x == 2: +        break +print(answer)