fun main(arr: Array<String>) {
    println(getAns()?:-1)
}


fun getAns():Int? {
    val n = readLine()!!.toInt()
    val list = (1..n).map { readLine()!!.toInt() }
    val dic = mutableMapOf<Int,Boolean>()
    var current = 1
    var ans = 0
    while (current != 2) {
        dic[current]?.let { return null }
        dic[current] = true
        ans++
        current = list[current-1]
    }
    return ans
}