import java.util.Scanner

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

  val n = sc.nextInt
  val al = List.fill(n)(sc.nextInt)

  val m = ((1 to n) zip al).toMap

  def loop(list: Vector[Int],num: Int):Vector[Int] ={
    val next = m(num)
    next match{
      case 1 => list
      case 2 => list :+ 2
      case _ => loop(list :+ num, next)
    }
  }

  val temp = loop(Vector.empty[Int], 1)
  val res = if(temp.contains(2)) temp.length else -1

  println(res)
}