object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val N = sc.nextInt()
  val A = ((0, false) :: List.fill(N)(sc.nextInt() -> false)).toArray
  println(loop(1, 1).getOrElse(-1))

  @scala.annotation.tailrec
  def loop(i: Int, count: Int): Option[Int] = A(i) match {
    case (2, _) => Some(count)
    case (_, true) => None
    case (j, false) =>
      A(i) = (i, true)
      loop(j, count + 1)
  }
}
