import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val edges = Array.tabulate(n){_ ⇒ Nil: List[Int]}
  val inEdge = Array.tabulate(n){_ ⇒ 0}
  for (_ ← 0 until m){
    val Array(x, y) = readLine.trim.split(' ').map(_.toInt)
    edges(x - 1) ::= y - 1
    inEdge(y - 1) += 1
  }
  var count = -1
  var currents: List[Int] = inEdge.indices.filter(p ⇒ inEdge(p) == 0).toList
  while (currents.nonEmpty) {
    count += 1
    var nexts = Nil: List[Int]
    for (c ← currents){
      for (n ← edges(c)){
        inEdge(n) -= 1
        if (inEdge(n) == 0){
          nexts ::= n
        }
      }
    }
    currents = nexts
  }
  println(count)
  implicit class Extend[T](value: T){
    def also(func: T ⇒ Unit): T = {func(value); value}
    def let[A](func: T ⇒ A): A = func(value)
  }
}