fun main(arr:Array<String>) {
    val itemCount = readLine()!!.toInt()
    val buttonMap = (1..itemCount).map { readLine()!!.toInt() - 1 }
    val flag:MutableList<Boolean> = (1..itemCount).map { false }.toMutableList()

    var current = 0
    var ans = 0
    while (current != 1) {
        if(flag[current]) {
            println(-1)
            return
        }
        ans++
        flag[current] = true
        current = buttonMap[current]
    }
    println(ans)
}
