# import sys

# input = sys.stdin.readline



N, *a = [int(x) for x in open(0)]

a.insert(0, None)



first = 1

pres = first

count = 0

nex =  a[pres] # press the button

count += 1

for i in range(N-1): # operations are at most N-1 times.

    if nex == 2: break

    prev = pres

    pres = nex

    nex = a[pres] # press the button

    count += 1

    if nex == prev or nex == pres: break



if nex == 2: print(count)

else: print((-1))
import sys



n, *b = list(map(int, sys.stdin.read().split()))



def main():

    on = 1

    lightened = set([1])

    for i in range(1, n):

        on = b[on-1]

        if on == 2:

            return i

        if on in lightened:

            return -1

        lightened.add(on) 



if __name__ == '__main__':

    ans = main()

    print(ans)
# import sys
# input = sys.stdin.readline
N, *a = [int(x) for x in open(0)]
a.insert(0, None)
first = 1
pres = first
count = 0
nex = a[pres]  # press the button
count += 1
for i in range(N - 1):  # operations are at most N-1 times.
    if nex == 2:
        break
    prev = pres
    pres = nex
    nex = a[pres]  # press the button
    count += 1
    if nex == prev or nex == pres:
        break
if nex == 2:
    print(count)
else:
    print((-1))

import sys

n, *b = list(map(int, sys.stdin.read().split()))


def main():
    on = 1
    lightened = set([1])
    for i in range(1, n):
        on = b[on - 1]
        if on == 2:
            return i
        if on in lightened:
            return -1
        lightened.add(on)


if __name__ == "__main__":
    ans = main()
    print(ans)

-# import sys -# input = sys.stdin.readline -N, *a = [int(x) for x in open(0)] -a.insert(0, None) -first = 1 -pres = first -count = 0 -nex = a[pres]  # press the button -count += 1 -for i in range(N - 1):  # operations are at most N-1 times. -    if nex == 2: -        break -    prev = pres -    pres = nex -    nex = a[pres]  # press the button -    count += 1 -    if nex == prev or nex == pres: -        break -if nex == 2: -    print(count) -else: -    print((-1)) +import sys + +n, *b = list(map(int, sys.stdin.read().split())) + + +def main(): +    on = 1 +    lightened = set([1]) +    for i in range(1, n): +        on = b[on - 1] +        if on == 2: +            return i +        if on in lightened: +            return -1 +        lightened.add(on) + + +if __name__ == "__main__": +    ans = main() +    print(ans)