stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

vector<int> pred[100010];

int f[100010];

int lp(int x){

	if(f[x]!=-1)

	   return f[x];

	for(int i=0;i<pred[x].size();i++){

		f[x]=max(f[x],lp(pred[x][i])+1);

	}

	f[x]=max(f[x],0);

	return f[x];

}

int main()

{

	cin>>n>>m;

	int a,b;

	for(int i=1;i<=m;i++){

		cin>>a>>b;

		pred[b].push_back(a);

	}

	int maxx=0;

	memset(f,-1,sizeof(f));

	for(int i=1;i<=n;i++){

		if(f[i]==-1)

	      f[i]=lp(i);

	    maxx=max(maxx,f[i]);

	}

	cout<<maxx<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.111378948801816x):
#include<bits/stdc++.h>

using namespace std;

typedef stack<int> stk;

int n,m;

vector<int> ga[100010];

int x,y;

int indeg[100010];

int dp[100010];

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>x>>y;

		ga[x].push_back(y);

		indeg[y]++;

	}

	stk S;

	for(int i=1;i<=n;i++){

		if(indeg[i]==0)

		   S.push(i);

	}

	int ans=0;

	while(!S.empty()){

		int cur=S.top();

		S.pop();

		ans=max(ans,dp[cur]);

		for(int i=0;i<ga[cur].size();i++){

			int nxt=ga[cur][i];

			dp[nxt]=max(dp[nxt],dp[cur]+1);

			indeg[nxt]--;

			if(indeg[nxt]==0)

			   S.push(nxt);

		}

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

int dp[100010];

vector<int> adj[100010];

int rec(int x){

	if(dp[x]>=0)

	   return dp[x];

	if(adj[x].empty())

	   return dp[x]=0;

	dp[x]=dp[adj[x][0]]+1;

	for(int i=0;i<adj[x].size();i++){

		dp[x]=max(rec(adj[x][i])+1,dp[x]);

	}

	return  dp[x];

}

int a,b;

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>a>>b;

		adj[a].push_back(b);

	}

	memset(dp,-1,sizeof(dp));

	int ans=0;

	for(int i=1;i<=n;i++)

	   ans=max(ans,rec(i));

	cout<<ans<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1111216112252953x):
#include<bits/stdc++.h>

using namespace std;

typedef stack<int> stk;

int n,m;

vector<int> ga[100010];

int x,y;

int indeg[100010];

int dp[100010];

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>x>>y;

		ga[x].push_back(y);

		indeg[y]++;

	}

	stk S;

	for(int i=1;i<=n;i++){

		if(indeg[i]==0)

		   S.push(i);

	}

	int ans=0;

	while(!S.empty()){

		int cur=S.top();

		S.pop();

		ans=max(ans,dp[cur]);

		for(int i=0;i<ga[cur].size();i++){

			int nxt=ga[cur][i];

			dp[nxt]=max(dp[nxt],dp[cur]+1);

			indeg[nxt]--;

			if(indeg[nxt]==0)

			   S.push(nxt);

		}

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

int dp[100010];

vector<int> radj[100010];

int rec(int x){

	if(dp[x]>=0)

	   return dp[x];

	if(radj[x].empty())

	   return dp[x]=0;

	dp[x]=dp[radj[x][0]]+1;

	for(int i=0;i<radj[x].size();i++){

		dp[x]=max(rec(radj[x][i])+1,dp[x]);

	}

	return  dp[x];

}

int a,b;

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>a>>b;

		radj[a].push_back(b);

	}

	memset(dp,-1,sizeof(dp));

	int ans=0;

	for(int i=1;i<=n;i++)

	   ans=max(ans,rec(i));

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1111216112252953x):
#include<bits/stdc++.h>

using namespace std;

typedef stack<int> stk;

int n,m;

vector<int> ga[100010];

int x,y;

int indeg[100010];

int dp[100010];

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>x>>y;

		ga[x].push_back(y);

		indeg[y]++;

	}

	stk S;

	for(int i=1;i<=n;i++){

		if(indeg[i]==0)

		   S.push(i);

	}

	int ans=0;

	while(!S.empty()){

		int cur=S.top();

		S.pop();

		ans=max(ans,dp[cur]);

		for(int i=0;i<ga[cur].size();i++){

			int nxt=ga[cur][i];

			dp[nxt]=max(dp[nxt],dp[cur]+1);

			indeg[nxt]--;

			if(indeg[nxt]==0)

			   S.push(nxt);

		}

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

vector<int> ga[100010];

int x,y;

int dp[100010];

int dypr(int u){

	if(dp[u]!=-1)

	   return dp[u];

	for(int i=0;i<ga[u].size();i++){

		int v=ga[u][i];

		dp[u]=max(dp[u],dypr(v)+1);

	}

	return dp[u];

}

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>x>>y;

		ga[y].push_back(x);

	}

	memset(dp,-1,sizeof(dp));

	for(int i=1;i<=n;i++)

	   dp[i]=dypr(i);

	cout<<*(max_element(dp+1,dp+n+1))+1;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1104732047289407x):
#include<bits/stdc++.h>

using namespace std;

typedef stack<int> stk;

int n,m;

vector<int> ga[100010];

int x,y;

int indeg[100010];

int dp[100010];

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>x>>y;

		ga[x].push_back(y);

		indeg[y]++;

	}

	stk S;

	for(int i=1;i<=n;i++){

		if(indeg[i]==0)

		   S.push(i);

	}

	int ans=0;

	while(!S.empty()){

		int cur=S.top();

		S.pop();

		ans=max(ans,dp[cur]);

		for(int i=0;i<ga[cur].size();i++){

			int nxt=ga[cur][i];

			dp[nxt]=max(dp[nxt],dp[cur]+1);

			indeg[nxt]--;

			if(indeg[nxt]==0)

			   S.push(nxt);

		}

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

vector<int> ga[100010];

int x,y;

int dp[100010];

int sol(int u){

	if(~dp[u])

	   return dp[u];

	if(ga[u].empty())

	   return dp[u]=0;

	for(int i=0;i<ga[u].size();i++){

		int v=ga[u][i];

		dp[u]=max(dp[u],sol(v)+1);

	}

	return dp[u];

}

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>x>>y;

		ga[x].push_back(y);

	}

	memset(dp,-1,sizeof(dp));

	for(int i=1;i<=n;i++)

	   sol(i);

	int ans=0;

	for(int i=1;i<=n;i++)

	   ans=max(ans,dp[i]);

	cout<<ans<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1112239280452973x):
#include<bits/stdc++.h>

using namespace std;

typedef stack<int> stk;

int n,m;

vector<int> ga[100010];

int x,y;

int indeg[100010];

int dp[100010];

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>x>>y;

		ga[x].push_back(y);

		indeg[y]++;

	}

	stk S;

	for(int i=1;i<=n;i++){

		if(indeg[i]==0)

		   S.push(i);

	}

	int ans=0;

	while(!S.empty()){

		int cur=S.top();

		S.pop();

		ans=max(ans,dp[cur]);

		for(int i=0;i<ga[cur].size();i++){

			int nxt=ga[cur][i];

			dp[nxt]=max(dp[nxt],dp[cur]+1);

			indeg[nxt]--;

			if(indeg[nxt]==0)

			   S.push(nxt);

		}

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

vector<int> pred[100010];

int f[100010];

int lp(int x){

	if(f[x]!=-1)

	   return f[x];

	for(int i=0;i<pred[x].size();i++){

		f[x]=max(f[x],lp(pred[x][i])+1);

	}

	f[x]=max(f[x],0);

	return f[x];

}

int main()

{

	cin>>n>>m;

	int a,b;

	for(int i=1;i<=m;i++){

		cin>>a>>b;

		pred[b].push_back(a);

	}

	int maxx=0;

	memset(f,-1,sizeof(f));

	for(int i=1;i<=n;i++){

		if(f[i]==-1)

	      f[i]=lp(i);

	    maxx=max(maxx,f[i]);

	}

	cout<<maxx<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1054591112132646x):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

		deg[v]++;

	}

	queue<int> q;

	for(int i=1;i<=n;i++)

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

		for(int i=0;i<ga[c].size();i++){

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

	cout<<*max_element(dp+1,dp+n+1)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

int dp[100010];

vector<int> adj[100010];

int rec(int x){

	if(dp[x]>=0)

	   return dp[x];

	if(adj[x].empty())

	   return dp[x]=0;

	dp[x]=dp[adj[x][0]]+1;

	for(int i=0;i<adj[x].size();i++){

		dp[x]=max(rec(adj[x][i])+1,dp[x]);

	}

	return  dp[x];

}

int a,b;

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>a>>b;

		adj[a].push_back(b);

	}

	memset(dp,-1,sizeof(dp));

	int ans=0;

	for(int i=1;i<=n;i++)

	   ans=max(ans,rec(i));

	cout<<ans<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1052031443633175x):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

		deg[v]++;

	}

	queue<int> q;

	for(int i=1;i<=n;i++)

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

		for(int i=0;i<ga[c].size();i++){

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

	cout<<*max_element(dp+1,dp+n+1)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

int dp[100010];

vector<int> radj[100010];

int rec(int x){

	if(dp[x]>=0)

	   return dp[x];

	if(radj[x].empty())

	   return dp[x]=0;

	dp[x]=dp[radj[x][0]]+1;

	for(int i=0;i<radj[x].size();i++){

		dp[x]=max(rec(radj[x][i])+1,dp[x]);

	}

	return  dp[x];

}

int a,b;

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>a>>b;

		radj[a].push_back(b);

	}

	memset(dp,-1,sizeof(dp));

	int ans=0;

	for(int i=1;i<=n;i++)

	   ans=max(ans,rec(i));

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1052031443633175x):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

		deg[v]++;

	}

	queue<int> q;

	for(int i=1;i<=n;i++)

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

		for(int i=0;i<ga[c].size();i++){

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

	cout<<*max_element(dp+1,dp+n+1)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

vector<int> ga[100010];

int x,y;

int dp[100010];

int dypr(int u){

	if(dp[u]!=-1)

	   return dp[u];

	for(int i=0;i<ga[u].size();i++){

		int v=ga[u][i];

		dp[u]=max(dp[u],dypr(v)+1);

	}

	return dp[u];

}

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>x>>y;

		ga[y].push_back(x);

	}

	memset(dp,-1,sizeof(dp));

	for(int i=1;i<=n;i++)

	   dp[i]=dypr(i);

	cout<<*(max_element(dp+1,dp+n+1))+1;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.10455819164945x):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

		deg[v]++;

	}

	queue<int> q;

	for(int i=1;i<=n;i++)

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

		for(int i=0;i<ga[c].size();i++){

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

	cout<<*max_element(dp+1,dp+n+1)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m;

vector<int> ga[100010];

int x,y;

int dp[100010];

int sol(int u){

	if(~dp[u])

	   return dp[u];

	if(ga[u].empty())

	   return dp[u]=0;

	for(int i=0;i<ga[u].size();i++){

		int v=ga[u][i];

		dp[u]=max(dp[u],sol(v)+1);

	}

	return dp[u];

}

int main()

{

	cin>>n>>m;

	for(int i=1;i<=m;i++)

	{

		cin>>x>>y;

		ga[x].push_back(y);

	}

	memset(dp,-1,sizeof(dp));

	for(int i=1;i<=n;i++)

	   sol(i);

	int ans=0;

	for(int i=1;i<=n;i++)

	   ans=max(ans,dp[i]);

	cout<<ans<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.105304916185632x):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

		deg[v]++;

	}

	queue<int> q;

	for(int i=1;i<=n;i++)

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

		for(int i=0;i<ga[c].size();i++){

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

	cout<<*max_element(dp+1,dp+n+1)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int dp[100100];

int n,m;

vector<int> ga[100100];

int u,v;

int d(int x){

	if(dp[x]!=-1)

		return dp[x];

	int &res=dp[x];

	res=0;

	for(int i=0;i<ga[x].size();i++){

		int y=ga[x][i];

		res=max(res,d(y)+1);

	}

	return res;

}

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

	}

	int res=0;

	memset(dp,-1,sizeof(dp));

	for(int i=1;i<=n;i++){

		res=max(res,d(i));

	}

	cout<<res<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1110471265983586x):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) (x).begin(),(x).end()

#define uniq(x) (x).erase(unique(ALL(x)),(x).end())

#define SORT(x) sort(ALL(x))

#define REV(x) reverse(ALL(x))

#define foreach(it,x) for(__typeof((x).begin()) it=(x).begin();it!=(x).end();it++)

#define debug(x) cerr<<#x<<"="<<(x)<<endl

#define gett() cerr<<"Time:"<<clock()<<"ms."<<endl

#define ri(x) scanf("%d",&x)

#define rl(x) scanf("%lld",&x)

#define rlcfpoj(x) scanf("%I64d",&x)

#define pi(x) printf("%d",x)

#define pl(x) printf("%lld",x)

#define plcfpoj(x) printf("%I64d",x)

#define lfd() printf("\n")

#define sp() printf(" ")

#define inrep(x) int x;scanf("%d",&x);while(x--)

#define testbit(mask,i) (((mask)>>(i))&1)

#define setbit(mask,i) ((mask)|(1<<i))

#define delbit(mask,i) ((mask)^(1<<i))

int n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		cin>>u>>v;

		ga[u].push_back(v);

		deg[v]++;

	}

	queue<int> q;

	for(int i=1;i<=n;i++)

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

		for(int i=0;i<ga[c].size();i++){

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

	cout<<*max_element(dp+1,dp+n+1)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

int dp[100005];

vector<int>v[100005];

int dfs(int x){

	if(dp[x]!=-1)return dp[x];

	if(v[x].size()==0)return dp[x]=0;

	for(int i=0;i<v[x].size();i++){

		dp[x]=max(dp[x],dfs(v[x][i])+1);

	}

	return dp[x];

}

int main(){

	int n,m;

	cin>>n>>m;

	for(int i=1;i<=m;i++){

		int a,b;

		cin>>a>>b;

		v[a].push_back(b);

	}

	memset(dp,-1,sizeof(dp));

	for(int i=1;i<=n;i++)dfs(i);

	int ans=0;

	for(int i=1;i<=n;i++)ans=max(ans,dp[i]);

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1115261787467083x):
#include<bits/stdc++.h>



using namespace std;

#define N_ 101000

int Deg[N_], Q[N_], head, tail, n, m, D[N_];

vector<int>E[N_];

int main() {

	int i, a, b;

	scanf("%d%d", &n,&m);

	for (i = 0; i < m; i++) {

		scanf("%d%d", &a, &b);

		E[a].push_back(b);

		Deg[b]++;

	}

	for (i = 1; i <= n; i++) {

		if (!Deg[i])Q[++tail] = i;

	}

	int res = 0;

	while (head < tail) {

		int x = Q[++head];

		res = max(res, D[x]);

		for(int y=0;y<E[x].size();y++) {

			Deg[E[x][y]]--;

			D[E[x][y]] = max(D[E[x][y]], D[x] + 1);

			if (!Deg[E[x][y]])Q[++tail] = E[x][y];

		}

	}

	printf("%d\n", res);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

typedef pair<int,int> P;

#define rep(i, n) for (ll i = 0; i < n; i++)

#define FOR(i, a, b) for (ll i = a; i < b; i++)

#define is(a, b) a == b

#define len(v) ll(v.size())



const ll mod=1e9+7;



//vector書き出し

template <class T>

void cout_vec(const vector<T> &vec1){

  rep(i,len(vec1)){

    cout<<vec1[i]<<' ';

  }

  cout<<'\n';

}



int n,m;

vector<int> g[100010];

vector<bool> used(100010,false);

vector<int> vi(100010);



void dfs(int v){

    if(used[v]){

        return;

    }

    used[v]=true;

    for(auto itr:g[v]){

        if(!used[itr]){

            dfs(itr);

        }

    }

    vi.push_back(v);

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin>>n>>m;

    rep(i,m){

        int x,y;

        cin>>x>>y;

        g[x].push_back(y);

    }

    vector<int> dp(100010,0);

    FOR(i,1,n+1){

        if(!used[i]){

            dfs(i);

        }

    }

    int ans=0;

    for(auto itr:vi){

        for(auto itr2:g[itr]){

            dp[itr]=max(dp[itr],dp[itr2]+1);

        }

        ans=max(ans,dp[itr]);

    }

    cout<<ans<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.354314082004168x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

#define rep(i, n) for (ll i = 0; i < n; i++)

#define FOR(i, a, b) for (ll i = a; i < b; i++)

#define len(v) ll(v.size())

#define fi first

#define se second



template <class T>

void cout_vec(const vector<T> &vec){

  for(auto itr:vec) cout<<itr<<' ';

  cout<<'\n';

}



typedef pair<ll,ll> P;

const ll mod=1e9+7;

const ll inf=1e15;



int n,m;

vector<vector<int>> g(100010);

vector<bool> used(100010,false);

vector<int> dp(100010,-1);



void dfs(int v){

  if(used[v]) return ;

  used[v]=true;

  if(g[v].empty()) dp[v]=0;

  for(auto itr:g[v]){

    dfs(itr);

    dp[v]=max(dp[v],dp[itr]+1);

  }

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin>>n>>m;

    rep(i,m){

      int x,y;

      cin>>x>>y;

      g[x].push_back(y);

    }

    FOR(i,1,n+1) if(!used[i]) dfs(i);

    cout<<*max_element(begin(dp),end(dp))<<endl;

}