import io.StdIn.readInt
import annotation.tailrec
import util.control.Breaks.{break, breakable}
import collection.mutable.{Seq => MutableSeq}

object Main extends App {
  val n = readInt
  val as = Seq.fill(n)(readInt)

  var pos = 0
  var res = 0
  var vs = MutableSeq.fill(n)(false)

  breakable {
    while (true)
    if (pos == 1) {
      println(res)
      break
    }
    else if (vs(pos)) {
      println(-1)
      break
    }
    else {
      vs(pos) = true
      pos = as(pos) - 1
      res = res + 1
    }
  }
}
