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

    val light = BooleanArray(n)

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