n = int(eval(input()))

a = [0]

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

a = a + tmp



r = -1

cnt = 1

next = a[1]

while cnt < n:

  if next == 2:

    r = cnt

    break

  else:

    next = a[next]

    cnt += 1

print(r)
import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

def main():

    n, *a = list(map(int, read().split()))

    r = 0

    x = 1

    while x != 2:

        r += 1

        x = a[x - 1]

        if x == 1 or r >= n:

            r = -1

            break

    print(r)



if __name__ == '__main__':

    main()
n = int(eval(input()))
a = [0]
tmp = [int(eval(input())) for _ in range(n)]
a = a + tmp
r = -1
cnt = 1
next = a[1]
while cnt < n:
    if next == 2:
        r = cnt
        break
    else:
        next = a[next]
        cnt += 1
print(r)

import sys

read = sys.stdin.read
readlines = sys.stdin.readlines


def main():
    n, *a = list(map(int, read().split()))
    r = 0
    x = 1
    while x != 2:
        r += 1
        x = a[x - 1]
        if x == 1 or r >= n:
            r = -1
            break
    print(r)


if __name__ == "__main__":
    main()

-n = int(eval(input())) -a = [0] -tmp = [int(eval(input())) for _ in range(n)] -a = a + tmp -r = -1 -cnt = 1 -next = a[1] -while cnt < n: -    if next == 2: -        r = cnt -        break -    else: -        next = a[next] -        cnt += 1 -print(r) +import sys + +read = sys.stdin.read +readlines = sys.stdin.readlines + + +def main(): +    n, *a = list(map(int, read().split())) +    r = 0 +    x = 1 +    while x != 2: +        r += 1 +        x = a[x - 1] +        if x == 1 or r >= n: +            r = -1 +            break +    print(r) + + +if __name__ == "__main__": +    main()