program trained
  implicit none
  integer :: n, a(100000) = 0, i, k
  read(*,*) n
  do i = 1, n
    read(*,*) a(i)
  end do
  i = 1
  do k = 1, n
    i = a(i)
    if (i == 2) then
      write(*,'(i0)') k
      stop
    end if
  end do
  write(*,'(i0)') -1
end program trained