import scala.io.StdIn
import scala.collection.mutable.Buffer
import scala.annotation.tailrec

object Main extends App {
  val n = StdIn.readInt
  val a = Iterator.continually(StdIn.readInt)
    .take(n)
    .toArray
    .toBuffer
  0 +=: a

  val visit = Array.ofDim[Boolean](n+1)
  @tailrec
  def rec(a:Buffer[Int],cnt:Int, from:Int, end:Int):Int = {
      val to = a(from)
      if(to == end) cnt
      else if(visit(from)) return -1
      else {
        visit(from) = true
        rec(a, cnt+1, to, end)
      }
  }

  val ans = rec(a, 1, 1, 2)

  println(ans)
}