// G - Longest Path
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;

int dfs(vvi &V, vi &D, int p){
	if(D[p]>0 || V[p].size()<=0) return D[p];
	for(int c:V[p]) D[p] = max(D[p], dfs(V,D,c)+1);
	return D[p];
}

int in(){int x;cin>>x;return x;}

int main(){
	int N,M; cin>>N>>M;

	vvi V(N+1);
	while(M--) V[in()].push_back(in());

	vi D(N+1);
	for(int p=1; p<=N; p++) D[0] = max(D[0], dfs(V,D,p));
	cout<< D[0] <<endl;
}