program main
  implicit none
  integer :: n, i, j
  integer, allocatable :: a(:)
  read(*, *) n
  allocate(a(n))
  read(*, *) (a(i), i = 1, n)
  j = 1
  do i = 1, n
    j = a(j)
    if (j == 2) then
      write(*, "(i0)") i
      stop 0
    end if
  end do
  write(*, "(i0)") -1
end program main

