import sys

sys.setrecursionlimit(500000)

N=int(eval(input()))

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

result=[False]*N

def next(k):

	newlight=A[k]

	if k==1:

		return(1)

	elif result[k]:

		return(0)

	else:

		result[k]=True

		return(next(newlight))

if not next(0):

	print((-1))

else:print((sum(result)))
N=int(eval(input()))

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

result=[False]*N

k=0

while 1:

	newlight=A[k]

	if k==1:

		ans=0

		break

	elif result[k]:

		ans=1

		break

	else:

		result[k]=True

		k=newlight

if ans:

	print((-1))

else:print((sum(result)))
import sys

sys.setrecursionlimit(500000)
N = int(eval(input()))
A = [int(eval(input())) - 1 for _ in range(N)]
result = [False] * N


def next(k):
    newlight = A[k]
    if k == 1:
        return 1
    elif result[k]:
        return 0
    else:
        result[k] = True
        return next(newlight)


if not next(0):
    print((-1))
else:
    print((sum(result)))

N = int(eval(input()))
A = [int(eval(input())) - 1 for _ in range(N)]
result = [False] * N
k = 0
while 1:
    newlight = A[k]
    if k == 1:
        ans = 0
        break
    elif result[k]:
        ans = 1
        break
    else:
        result[k] = True
        k = newlight
if ans:
    print((-1))
else:
    print((sum(result)))

-import sys - -sys.setrecursionlimit(500000) - - -def next(k): +k = 0 +while 1: -        return 1 +        ans = 0 +        break -        return 0 +        ans = 1 +        break -        return next(newlight) - - -if not next(0): +        k = newlight +if ans: