def main():

    N = int(eval(input()))

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



    ans = 0

    nxt = 1

    while True:

        if ans > N:

            print((-1))

            exit()

        elif nxt == 2:

            break

        nxt = a[nxt-1]

        ans += 1

    print(ans)





if __name__ == "__main__":

    main()

def main():

    N = int(eval(input()))

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



    tmp = 0



    for i in range(N):

        if a[tmp] == 2:

            print((i+1))

            exit()

        else:

            tmp = a[tmp]-1



    print((-1))





if __name__ == "__main__":

    main()

def main():
    N = int(eval(input()))
    a = [int(eval(input())) for _ in range(N)]
    ans = 0
    nxt = 1
    while True:
        if ans > N:
            print((-1))
            exit()
        elif nxt == 2:
            break
        nxt = a[nxt - 1]
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()

def main():
    N = int(eval(input()))
    a = [int(eval(input())) for _ in range(N)]
    tmp = 0
    for i in range(N):
        if a[tmp] == 2:
            print((i + 1))
            exit()
        else:
            tmp = a[tmp] - 1
    print((-1))


if __name__ == "__main__":
    main()

-    ans = 0 -    nxt = 1 -    while True: -        if ans > N: -            print((-1)) +    tmp = 0 +    for i in range(N): +        if a[tmp] == 2: +            print((i + 1)) -        elif nxt == 2: -            break -        nxt = a[nxt - 1] -        ans += 1 -    print(ans) +        else: +            tmp = a[tmp] - 1 +    print((-1))