#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,x,y;
vector<ll>v[100009];
ll dp[100009];

ll f(ll N){
	if (v[N].empty())return 0;
	if(dp[N]!=-1)return dp[N];
	ll z=0;
	for(int i=0; i<v[N].size(); i++){
		z=max(z,f(v[N][i])+1);
	}
	dp[N]=z;
	return dp[N];
}

int main(){
	cin >> n >> m;
	memset(dp,-1,sizeof dp);
	for(int i=1; i<=m; i++){
		cin >> x >> y;
		v[x].push_back(y);
	}
	ll jwb=0;
	for(int i=1; i<=n; i++){
		jwb=max(jwb,f(i));
	}
	cout << jwb << endl;
}//j