console.log(function(args){
  const [[N,M],...tmp] = args.trim().split('\n').map( r=>r.split(' ').map(v=>v|0) );
  const d = Array(N).fill(0), stack = [];
  for ( let [x,y] of tmp ) {
    d[x-1] = NaN;
    stack.push([x-1,y-1]);
  }
  do {
    const [x,y] = stack.pop();
    if ( isFinite(d[y]) ) d[x] = Math.max( d[x]|0, d[y]+1 );
    else stack.unshift([x,y]);
  } while( stack[0] );
  return d.reduce((a,b)=>Math.max(a,b|0)).toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
