import java.util.*

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

    val graph = Graph1(n)
    repeat(m) {
        val (x, y) = readLine()!!.split(' ').map { it.toInt() }
        graph.add(x - 1, y - 1)
    }

    println(graph.solve())
}

class Graph1 constructor(private val n: Int) {
    private val graph = Array(n) { mutableListOf<Int>() }
    private val dp = IntArray(100000) { -1 }

    fun add(x: Int, y: Int) {
        graph[x].add(y)
    }

    private 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 solve(): Int {
        var res = 0
        (0 until n).forEach {
            res = Math.max(res, rec(it))
        }
        return res
    }

}

class Graph2 constructor(private val n: Int) {
    private val graph = Array(n) { mutableListOf<Int>() }
    private val deg = IntArray(n) { 0 }
    private val dp = IntArray(100000) { 0 }

    fun add(x: Int, y: Int) {
        graph[x].add(y)
        deg[y]++
    }

    fun solve(): Int {

        val queue: Queue<Int> = ArrayDeque<Int>()
        deg.forEachIndexed { index, i ->
            if (i == 0) {
                queue.add(index)
            }
        }

        while (queue.isNotEmpty()) {
            val v = queue.remove()
            graph[v].forEach {
                deg[it]--
                if (deg[it] == 0) {
                    queue.add(it)
                    dp[it] = Math.max(dp[it], dp[v] + 1)
                }
            }
        }

        var res = 0
        (0 until n).forEach {
            res = Math.max(res, dp[it])
        }
        return res
    }

}