program main
	implicit none
	integer N, i, j, counter
	integer, allocatable :: a(:)
	read(*, *) N
	allocate(a(1: N))
	do i = 1, N
		read(*, *) a(i)
	end do
	i = 1
	counter = 0
	do j = 1, N
		i = a(i)
		counter = counter + 1
		if(i == 2) then
			write(*, *) counter
			exit
		else if(j == N) then
			write(*, *) -1
			exit
		end if
	end do	
end program main