import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*

fun main(omkar: Array<String>) {
    val jin = BufferedReader(InputStreamReader(System.`in`))
    var line = jin.readLine()
    var six = line.indexOf(' ')
    val n = line.substring(0, six).toInt()
    val m = line.substring(six + 1).toInt()
    val rev = Array(n + 1) { mutableListOf<Int>() }
    for (j in 1..m) {
        line = jin.readLine()
        six = line.indexOf(' ')
        val a = line.substring(0, six).toInt()
        val b = line.substring(six + 1).toInt()
        rev[b].add(a)
    }
    val dp = IntArray(n + 1) { -2 }
    val stack = Stack<Int>()
    for (j in 1..n) {
        stack.push(j)
    }
    var answer = 0
    while (stack.isNotEmpty()) {
        val a = stack.pop()
        if (dp[a] == -2) {
            dp[a] = -1
            stack.push(a)
            stack.addAll(rev[a])
        } else {
            if (dp[a] == -1) {
                dp[a] = 0
                for (b in rev[a]) {
                    dp[a] = Math.max(dp[a], dp[b] + 1)
                }
            }
            answer = Math.max(answer, dp[a])
        }
    }
    println(answer)
}