fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: return
    val a = 0.until(n).map { readLine()?.toInt() ?: return }
    val m = Array(n) { false }
    var c = 0
    var p = a.first()
    while (true) {
        c++
        if (p == 2) {
            println(c.toString())
            return
        } else {
            if (m[p - 1]) {
                println("-1")
                return
            } else {
                m[p - 1] = true
                p = a[p - 1]
            }
        }
    }
}
