n = int(eval(input()))

lst = [int(eval(input())) for _ in range(n)]

i = lst[0]

for c in range(n):

    if i == 2:

        print((c + 1))

        quit()

    i = lst[i - 1]

print((-1))
import sys



n = int(eval(input()))

lst = [int(i) for i in sys.stdin]

i = lst[0]

for c in range(n):

    if i == 2:

        print((c + 1))

        quit()

    i = lst[i - 1]

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

import sys

n = int(eval(input()))
lst = [int(i) for i in sys.stdin]
i = lst[0]
for c in range(n):
    if i == 2:
        print((c + 1))
        quit()
    i = lst[i - 1]
print((-1))

+import sys + -lst = [int(eval(input())) for _ in range(n)] +lst = [int(i) for i in sys.stdin]