fun main(args: Array<String>) {
    val N = readLine()!!.toInt()
    val A = Array(N, {i -> readLine()!!.toInt()})

    tailrec fun next(n: Int, count: Int): Int =
        if (n==2) count
        else if (count >= 1e5+10) -1
        else next(A[n-1], count+1)

    println(next(1, 0))
}