fun main(args: Array<String>) {
    val n= readLine()!!.toInt()
    val a=(1..n).map { readLine()!!.toInt()-1 }
    val visited = BooleanArray(n){ false }
    visited[0]=true

    var light=0
    var count=0
    while (light!=1){

        light=a[light]
        count++
        if(visited[light]){
            println(-1)
            return
        }else{
            visited[light]=true
        }
    }
    println(count)
}