import std.stdio, std.string, std.algorithm, std.range, std.conv;

void main() {
  long N, M;
  readf(" %d %d ", &N, &M);
  
  auto X = new long[M], Y = new long[M];
  foreach (i; 0..M) {
    readf(" %d %d ", &X[i], &Y[i]);
    --X[i];
    --Y[i];
  }
  
  auto dp = new long[N];
  dp[] = -1;
  long rec(long target) {
    if (dp[target] >= 0) return dp[target];
    
    dp[target] = 0;
    foreach (m; zip(X, Y)) {
      if (m[0] != target) continue;
      dp[target] = dp[target].max(rec(m[1]) + 1);
    }
    return dp[target];
  }
  
  long ans;
  foreach (i; 0..N) {
    ans = ans.max(rec(i));
  }
  ans.writeln;
}