/* long long or int */
/* inline & usighed & sighned main */
/* xx 396*/

#include<bits/stdc++.h>
using namespace std;

const int MAX_N=1e5+10;

vector <int> G[MAX_N];
queue <int> Q;
int dist[MAX_N],deg[MAX_N],N,M,ans=0;

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin>>N>>M;
	for(int i=0;i<M;i++){
		int X,Y; cin>>X>>Y; X--; Y--;
		G[X].push_back(Y); deg[Y]++;
	}
	for(int i=0;i<N;i++) if(deg[i]==0) Q.push(i);
	while(!Q.empty()){
		int fr=Q.front(); ans=max(ans,dist[fr]); Q.pop();
		for(int i=0;i<G[fr].size();i++){
			int to=G[fr][i]; deg[to]--;
			dist[to]=max(dist[to],dist[fr]+1);
			if(deg[to]==0) Q.push(to);
		}
	}
	cout<<ans<<endl;
	return 0;
}