class Graph<T : List<Int>> constructor(private val graph: Array<T>) {
    private val dp = IntArray(100000) { -1 }

    fun rec(v: Int): Int {
        if (dp[v] != -1) return dp[v]

        var res = 0
        graph[v].forEach {
            res = Math.max(res, rec(it) + 1)
        }
        dp[v] = res
        return res
    }

}

fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(' ').map { it.toInt() }

    val graph = Array(n) { mutableListOf<Int>() }
    repeat(m) {
        val (x, y) = readLine()!!.split(' ').map { it.toInt() }
        graph[x - 1].add(y - 1)
    }

    val g = Graph(graph)
    var res = 0
    (0 until n).forEach {
        res = Math.max(res, g.rec(it))
    }
    println(res)
}
