implicit none
integer :: n,i,count,bo,bn
integer,allocatable :: a(:)
 
read(*,*) n
allocate(a(n))
do i=1,n
read(*,*) a(i)
end do
 
count=0
bo=a(1)
bn=0
do while(bn==2.or.bo<0)
bn=a(bo)
count=count+1
a(bo)=-1
bo=bn
end do
 
if(bn<0)then
write(*,*) bn
else
write(*,*) count
endif
 
end