import sys



N = int(eval(input()))

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



l = 1

p = a[0] - 1



if a[0] == 2:

    print(l)

    sys.exit()



while l < int(1e6):

    l += 1

    if a[p] == 2:

        print(l)

        break

    else:

        p = a[p] - 1

else:

    print((-1))
import sys



N = int(eval(input()))

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



l = 1

p = a[0] - 1



if a[0] == 2:

    print(l)

    sys.exit()



while l < int(2e5):

    l += 1

    if a[p] == 2:

        print(l)

        break

    else:

        p = a[p] - 1

else:

    print((-1))
import sys

N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
l = 1
p = a[0] - 1
if a[0] == 2:
    print(l)
    sys.exit()
while l < int(1e6):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print((-1))

import sys

N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
l = 1
p = a[0] - 1
if a[0] == 2:
    print(l)
    sys.exit()
while l < int(2e5):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print((-1))

-while l < int(1e6): +while l < int(2e5):