#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+3;
vector<int>adj[maxn];
int dp[maxn];

int  dfs(int v)
{
	if(dp[v]!=-1) return dp[v];
	int res=0;
	for(auto j:adj[v])
	{
		res=max(res,1+dfs(j));
	}
	dp[v]=res;
	return dp[v];
}
int main ()
{
	int n,m;
	cin>>n>>m;
	int x,y;
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d",&x,&y);
		adj[x].push_back(y);
	}
	memset(dp,-1,sizeof(dp));
	int max_path=0;
	for(int i=1;i<=n;i++)
	{
		max_path=max(max_path,dfs(i));
	}
	cout<<max_path<<endl;

}