import scala.io.StdIn

object Main extends App {

  val n = StdIn.readLine().toInt
  val a = (0 until n).map(_ => StdIn.readLine().toInt)
  val ap = Array.fill(n)(0)

  def solve(i: Int, cnt: Int): Int = {
    if (ap(i) == 1) -1
    else if (a(i) == 2) cnt + 1
    else {
      ap(i) = 1
      solve(a(i) - 1, cnt + 1)
    }
  }
  println(solve(0, 0))

}
