#include<bits/stdc++.h>
using namespace std;
using LL=long long;
vector<int>from[100001];
vector<int>dp;
int df(int i){
  if(dp[i]!=-1)
    return dp[i];
  dp[i]=0;
  for(int f:from[i])
    dp[i]=max(dp[i],df(f)+1);
  return dp[i];
}
int main(){
  int N,M;cin>>N>>M;
  for(int i=0;i<M;i++){
    int x,y;cin>>x>>y;
    from[y].push_back(x);
  }
  dp.resize(N+1,-1);
  for(int i=1;i<=N;i++)
    if(dp[i]==-1)df(i);
  cout<<*max_element(dp.begin(),dp.end())<<endl;
}