#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr(i,n) for(int i=0;i++<n;)
#define Fr(i,n) for(int i=0;i<n;i++)

int n,m,x,y,dp[100010]={};
vector<int> e[100010]={};

int M(int i){
  if(dp[i]!=0) return dp[i];
  int s=e[i].size(),k=1;
  Fr(j,s) k=max(k,1+M(e[i][j]));
  dp[i]=k;
  return k;
}

int main(){
  cin>>n>>m;
  fr(i,m){cin>>x>>y;e[x].pb(y);}
  int ans=0;
  fr(i,n) ans=max(ans,M(i));
  cout<<ans-1<<endl;
}