object Main {

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

    val Inf = Int.MaxValue / 10
    val dp = Array.fill(N)(Inf)
    val stack = new scala.collection.mutable.Stack[Int]
    (0 until N) foreach { u =>
      if (dp(u) == Inf) {
        stack.push(u)
        while (stack.nonEmpty) {
          val u = stack.pop()
          if (u < 0) {
            dp(-u) = 0
            G(-u).foreach { v =>
              dp(-u) = dp(-u) max (dp(v) + 1)
            }
            dp(-u)
          } else {
            stack.push(-u)
            G(u).filter(_ == Inf)
              .foreach(stack.push)
          }
        }
      }
    }
    println(dp.max)
  }

}