console.log(function(args){
  const [[N,M],...tmp] = args.trim().split('\n').map( r=>r.split(' ').map(v=>v|0) );
  const e = Array(N).fill(0).map(_=>[]), d = Array(N).fill(0);
  for ( let [x,y] of tmp ) {
    e[x-1].push( y-1 );
    d[x-1] = NaN;
  }
  function* dfs(c) {
    if ( isFinite(d[c]) ) return yield d[c];
    for ( const n of e[c] ) {
      for ( const v of dfs(n) ) yield v+1;
    }
  }
  let max = 0;
  for ( let i = 0; i < N; i++ ) {
    for ( let k of dfs(i) ) {
      if ( max < k ) d[i] = max = k;
    }
  }
  return max.toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));