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[y-1]++;
  }
  const queue = [], dp = Array(N).fill(0);
  for ( let i = 0; i < N; i++ ) if ( !d[i] ) queue.push(i);
  while ( queue.length ) {
    const v = queue.shift();
    for ( const child of e[v] ) {
      d[child]--;
      if ( d[child] === 0 ) {
        queue.push( child );
        if ( dp[child] < dp[v] + 1 ) dp[child] = dp[v] + 1;
      }
    }
  };
  return dp.reduce((a,b)=>Math.max(a,b|0)).toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
