fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: return
    val aList = (0 until n).map {
        readLine()?.toInt()!!
    }
    var pos = 1
    var count = 1
    while (aList[pos - 1] != 2 && count < n) {
        pos = aList[pos - 1]
        count++
    }
    println(if (aList[pos - 1] == 2) count else -1)
}