fun main(arr:Array<String>) {
    val buttonCount = readLine()!!.toInt()
    val buttons = (1..buttonCount).map { readLine()!!.toInt()-1 }

    var current = 0
    for(i in (0..buttonCount)) {
        if(current == 1) {
            println(i)
            return
        }
        current = buttons[current]
    }
    println(-1)
}
