#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,dp[100009],ans;
vector<vector<ll> >v;
ll dfs(ll d)
{
    if(dp[d]!=-1)
    return dp[d];
    dp[d]=0;
    for(auto z:v[d])
    dp[d]=max(dp[d],dfs(z)+1);
    return dp[d];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
v.resize(n+1);
while(m--){
    ll x,y;
    cin>>x>>y;
    v[x].push_back(y);
}
memset(dp,-1,sizeof dp);
for(ll i=1;i<=n;i++)
ans=max(ans,dfs(i));
cout<<ans;
return 0;
}
