object Main extends App {
  val input = io.Source.stdin.getLines.toIndexedSeq.map(_.toInt)
  
  private def func():Int={
  	var i=1
    var next=1
    var visited=Set[Int]()
    while(!visited.contains(next)){
      visited = visited ++ Set(next)
      next=input(next)
      if(next == 2) return i      
      i += 1
    }
    -1
  }
  println(func())
}
