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), stack = [];
  for ( let [x,y] of tmp ) {
    e[x-1].push( y-1 );
    d[x-1] = NaN;
    stack.push([x-1,y-1]);
  }
  while ( stack[0] ) {
    const [c,n] = stack.pop();
    if ( isFinite(d[n]) ) {
      d[c] = Math.max( d[c]|0, d[n]+1 );
      continue;
    }
    for ( const next of e[n] ) {
      if ( isFinite(d[next]) ) d[n] = Math.max( d[n]|0, d[next]+1 );
      else stack.push([n,next]);
    }
  }
  return Math.max(...d).toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));