program sample
    implicit none
  
    integer(8) :: i,j,m,n,h,w
    
    integer(8),allocatable :: a(:),y(:)
  
    read(*,*) n
    allocate(a(n))
    read(*,*)a
    w=1
    do i=1,n
        w=a(w)
        if (w==2)then
            write(*,*)i
            stop
        end if
    end do
    write(*,*)-1
    stop
end program sample
  

