#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> gr[100005];
ll ans=0;
ll dfs(ll curr,vector<ll> &dp)
{
	ll d=0;
	if(dp[curr]!=-1)
		return dp[curr];
	for(auto it:gr[curr])
	{
		d=max(d,dfs(it,dp));
	}
	return dp[curr]=1+d;
}
int main()
{
	ll n,m,x,y;
	cin>>n>>m;
	vector<ll> dp(n+1,-1);
	for(ll i=0;i<m;i++)
	{
		cin>>x>>y;
		gr[x].push_back(y);
	}
	for(ll i=1;i<=n;i++)
	{
		ans=max(ans,dfs(i,dp));
	}
	cout<<ans-1;
}