def process(a):

    tmp = a[0]

    pushed = set()

    cnt = 1

    while not tmp in pushed:

        if tmp == 2:

            return cnt

        pushed.add(tmp)

        tmp = a[tmp - 1]

        cnt += 1

    return -1



if __name__ == '__main__':

    a = [int(input().strip()) for _ in range(int(input().strip()))]

    print((process(a)))
import sys



def process(a):

    tmp = a[1]

    pushed = set()

    cnt = 1

    while not tmp in pushed:

        if tmp == 2:

            return cnt

        pushed.add(tmp)

        tmp = a[tmp]

        cnt += 1

    return -1



if __name__ == '__main__':

    # a = [int(input().strip()) for _ in range(int(input().strip()))]

    a = list(map(int, sys.stdin))

    print((process(a)))
def process(a):
    tmp = a[0]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1


if __name__ == "__main__":
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print((process(a)))

import sys


def process(a):
    tmp = a[1]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp]
        cnt += 1
    return -1


if __name__ == "__main__":
    # a = [int(input().strip()) for _ in range(int(input().strip()))]
    a = list(map(int, sys.stdin))
    print((process(a)))

+import sys + + -    tmp = a[0] +    tmp = a[1] -        tmp = a[tmp - 1] +        tmp = a[tmp] -    a = [int(input().strip()) for _ in range(int(input().strip()))] +    # a = [int(input().strip()) for _ in range(int(input().strip()))] +    a = list(map(int, sys.stdin))