import java.util.*

fun main(args: Array<String>) {
    val N = readLine()!!.toInt()
    val a = IntArray(N) { if (it == 0) -1 else readLine()!!.toInt() - 1 }
    val children = Array(N) { ArrayList<Int>() }

    for (i in 1 until a.size) {
        children[a[i]].add(i)
    }

    fun dfs(v: Int): Int{
        val depth = ArrayList<Int>()

        for (u in children[v]) {
            depth.add(dfs(u))
        }

        depth.sortByDescending { it }

        return depth.mapIndexed { index, d -> d + index + 1 }.max() ?: 0
    }

    println(dfs(0))
}
