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

    val visit = mutableSetOf(1)
    var b = 1
    var count = 0
    while (true) {
        b = a[b-1]
        if(b in visit) return@run -1
        visit.add(b)
        count++
        if(b == 2) return@run count
    }

})
