import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)

  val start = System.currentTimeMillis

  val n = sc.nextInt
  val aArr = Array.fill(n)(sc.nextInt)

  def loop(cnt: Int,num: Int):Int ={
    val next = aArr(num - 1)
    next match{
      case 1 => -1
      case 2 => cnt + 1
      case _ => loop(cnt + 1, next)
    }
  }

  val res = loop(0, 1)

  println(res)
}
