import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  // val ar = new Array[Int](N)

  val N, M = in.next().toInt
  val graph = Array.fill[List[Int]](N+1)(Nil)
  for(_ <- 1 to M) {
    val a, b = in.next().toInt
    graph(a) = b :: graph(a)
  }


  //
  val dp = Array.fill[Int](N+1)(-1)

  def calc(i: Int): Int = {
    if(dp(i) == -1) {
      var res = 0
      for(e <- graph(i)){
        calc(e)
        res = Math.max(res, dp(e)+1)
      }
      dp(i) = res
    }
    dp(i)
  }

  
  for (i <- 1 to N) {
    calc(i)
  }


  println(dp.max)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
