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]);
  }
  do {
    const [tree,n] = stack.pop();
    if ( isFinite(d[n]) ) {
      let s = d[n];
      for ( let a of tree ) {
        if ( (d[a]|0) <= s + 1 ) d[a] = ++s;
        else break;
      }
    } else {
      for ( let c of e[n] ) {
        stack.push([[n].concat(tree),c]);
      }
    }
  } while( stack[0] );
  return d.reduce((a,b)=>Math.max(a,b|0)).toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
