object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val list = List.fill(n)(sc.nextInt)
  
  def loop(num: Int, count: Int): Int = {
    if (num == 2) count
    else if (count > n) -1
    else loop(list(num - 1), count + 1)
  }
  
  println(loop(1, 0))
}