object Main {

  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)
    val N, M = sc.nextInt()
    @inline val G = Array.fill(N)(new scala.collection.mutable.ArrayBuffer[Int]())
    (0 until M) foreach { _ =>
      val u, v = sc.nextInt()
      G(u-1).append(v-1)
    }

    val Inf = Int.MaxValue / 10
    @inline val dp = Array.fill(N)(Inf)
    def recursive(u: Int): Int =
      if (dp(u) != Inf) dp(u)
      else {
        dp(u) = 0
        G(u).foreach { v =>
          dp(u) = (recursive(v) + 1) max dp(u)
        }
        dp(u)
      }

    (0 until N) foreach { u =>
      if (dp(u) == Inf)
        recursive(u)
    }
    println(dp.max)
  }

}