n = int(eval(input()))

a = list(map(int, list(eval(input()) for _ in range(n))))

visited = list(False for _ in range(n))



if (2 in a) == False:

    print((-1))

    exit()

    

idx = 1

cnt = 0

while True:

    cnt += 1

    if a[idx - 1] == 2:

        print(cnt)

        exit()

    else:

        if visited[idx - 1] == True:

            print((-1))

            exit()

        else:

            visited[idx - 1] = True

            idx = a[idx - 1] ##次のボタン
def main():

    import sys

    input = sys.stdin.readline



    N = int(eval(input()))

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



    cur = 0

    cnt = [-1] * N

    cnt[0] = 0

    while cur != 1:

        c = cnt[cur]



        cur = a[cur]

        if ~cnt[cur]: break

        cnt[cur] = c + 1



    print((cnt[1] if ~cnt[1] else -1))





if __name__ == '__main__':

    main()

n = int(eval(input()))
a = list(map(int, list(eval(input()) for _ in range(n))))
visited = list(False for _ in range(n))
if (2 in a) == False:
    print((-1))
    exit()
idx = 1
cnt = 0
while True:
    cnt += 1
    if a[idx - 1] == 2:
        print(cnt)
        exit()
    else:
        if visited[idx - 1] == True:
            print((-1))
            exit()
        else:
            visited[idx - 1] = True
            idx = a[idx - 1]  ##次のボタン

def main():
    import sys

    input = sys.stdin.readline
    N = int(eval(input()))
    a = [int(eval(input())) - 1 for _ in range(N)]
    cur = 0
    cnt = [-1] * N
    cnt[0] = 0
    while cur != 1:
        c = cnt[cur]
        cur = a[cur]
        if ~cnt[cur]:
            break
        cnt[cur] = c + 1
    print((cnt[1] if ~cnt[1] else -1))


if __name__ == "__main__":
    main()

-n = int(eval(input())) -a = list(map(int, list(eval(input()) for _ in range(n)))) -visited = list(False for _ in range(n)) -if (2 in a) == False: -    print((-1)) -    exit() -idx = 1 -cnt = 0 -while True: -    cnt += 1 -    if a[idx - 1] == 2: -        print(cnt) -        exit() -    else: -        if visited[idx - 1] == True: -            print((-1)) -            exit() -        else: -            visited[idx - 1] = True -            idx = a[idx - 1]  ##次のボタン +def main(): +    import sys + +    input = sys.stdin.readline +    N = int(eval(input())) +    a = [int(eval(input())) - 1 for _ in range(N)] +    cur = 0 +    cnt = [-1] * N +    cnt[0] = 0 +    while cur != 1: +        c = cnt[cur] +        cur = a[cur] +        if ~cnt[cur]: +            break +        cnt[cur] = c + 1 +    print((cnt[1] if ~cnt[1] else -1)) + + +if __name__ == "__main__": +    main()