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

#include <algorithm>

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstring>

#include <vector>

#include <string>

#include <climits>

#include <map>

#include <set>

#include <stack>

#include <queue>

#include <deque>

#include <stdio.h>

//#define int long long

using namespace std;

#define loop(n) for(int i=0;i<n;i++)

#define endl '\n'

#define OnlineJudgec

#define F first

#define S second

#define  precision(n)  cout << fixed << setprecision(n)

#define clr(v,d ) memset(v, d, sizeof(v))

typedef long long ll;

typedef vector<int> vi;

void init()

{

    cin.tie(0);

    cin.sync_with_stdio(0);

    cout.tie(0);

    #ifdef OnlineJudge

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

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

    #endif

}

ll S(ll x){ll s = 0;while(x){s += x % 10;x /= 10;}return s;} /// sum of digits.

ll powe(ll a , ll n){if(n == 1)return a;return a * powe(a ,n-1);}

ll com(int n,int m){ll z = 1;for(int i = 0; i < m;i++){z = z * ( n - i) / (i + 1) ;}return z;}

int dx[] = {1,-1,0,0};

int dy[] = {0,0,1,-1};

int dr[] = {0,0,0,1,1,1,-1,-1,-1};

int dc[] ={-1,0,1,-1,0,1,-1,0,1};

/// <<------------------------------------------------------------------------------>>

const int N = 100009;

int n,m;

int MOD = int(1e9) + 7;

int dp[N];

vector<vi>g(N);

int dfs(int node)

{

    int &rst = dp[node];

    if(~rst)

        return rst;

    rst  = 0;

    for(int neighbor : g[node])

    {

        rst =max(rst , 1 + dfs(neighbor));

    }

    return rst;

}

/// <<------------------------------------------------------------------------------>>

int main()

{

    init();

    clr(dp,-1);

    cin >> n >> m;

    loop(m)

    {

        int l ,r;

        cin >> l >> r;

        g[l].push_back(r);

    }

    int ans = 0;

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

    {

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

    }

    cout<<ans<<endl;

}


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



using namespace std;



const int N = 1e5+7;

int n, m, grau[N], dist[N];

vector<int> g[N];



int main(){

#ifdef LOCAL

	freopen("input", "r", stdin);

#endif



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



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

		int a, b;

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

		grau[b]++;

		g[a].push_back(b);

	}



	queue<int> q;



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

		if (grau[i] == 0)

			q.push(i);

	}



	int ans = 0;



	while (!q.empty()){

		int at = q.front();

		//cout << at << endl;

		q.pop();

		for(int i = 0; i < (int)g[at].size(); i++){

			int to = g[at][i];

			if (grau[to]){ 

				grau[to]--;

				if (grau[to] == 0){

					dist[to] = dist[at] + 1;

					ans = max(ans, dist[to]);

					q.push(to);

				}

			}

		}

	}



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



	return 0; 

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

#define fi first

#define se second

#define pb push_back

#define mp make_pair

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

using namespace std;

typedef long long LL;

typedef pair<int,int> pii;

const int INF=0x3f3f3f3f;

const int N=1e5+10;



inline int read(){

	int ww=1,xx=0;char ch=getchar();

	while(!isdigit(ch)){if(ch=='-') ww=-ww;ch=getchar();}

	while(isdigit(ch)){xx=(xx<<1)+(xx<<3)+(ch^48);ch=getchar();}

	return ww*xx;

}



int n,m,fr,to,cnt;

int de[N],ans;

int q[N],l,r=-1;

int head[N];

int f[N];

vector<int> ed[N];



signed main(){

//  freopen(".in","r",stdin);

//  freopen(".out","w",stdout);

	n=read();

	m=read();

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

		fr=read();

		to=read();

		de[to]++;

		ed[fr].pb(to);

	}

	r=-1;

	l=0;

//	for(int i=1;i<=n;i++) printf("%d ",de[i]);

	for(int i=1;i<=n;i++) if(de[i]==0) q[++r]=i;

	while(l<=r){

		int u=q[l++];

//		printf("%d ",u);

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

			int v=ed[u][i];

			de[v]--;

			if(de[v]==0) q[++r]=v;

		}

	}

//	puts("");

	for(int i=0;i<r;i++){

		int u=q[i];

//		printf("%d ",u);

		for(int j=0;j<ed[u].size();j++)

			f[ed[u][j]]=max(f[ed[u][j]],f[u]+1);

	}

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

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

	return 0;

}

/*

4 5

1 2

1 3

3 2

2 4

3 4

*/

// Faster C++ solution for the same problem (measured speedup: 3.4832002417656924x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;



const int MAXN=100005;

vector <int> E[MAXN];

int In[MAXN];

int Q[MAXN], head, tail;

int n, m, dp[MAXN];



int main()

{

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

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

		int a, b;

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

		E[a].push_back (b);

		In[b]++;

	}

	

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

		if (!In[i])

			Q[++tail] = i;

	

	int res=0;

	while (head < tail){

		int x=Q[++head];

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

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

			int y=E[x][i];

			In[y]--;

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

			if (!In[y])  Q[++tail] = y;

		}

	}

	

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.4857030892553453x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

#define max3(a,b,c) max(a,max(b,c))

const ll INF = 1 << 30;

typedef pair<double, double  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<double,payar  > > vpp;

vector < payar >  vp;

vector<int>g[100005];

int ind[100005];

int vis[100005];

int dis[100005];



int dfs(int u)

{



    vis[u]=1;

    if(dis[u]!=-1)return dis[u];

    int maxi =0;

    for(auto it: g[u])

    {

       // static int k;

     //   cout<<k++<<" "<<it<<endl;



      dis[u]=  maxi=max(maxi,1+dfs(it) );

    }

    return maxi;

}

int main()

{



    int n, m ;

    cin>>n>>m;

    memset(dis,-1,sizeof dis);

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

    {

        int u, v;

        cin>>u>>v;

        g[u].push_back(v);

        ind[v]++;



    }

    int ans =0;

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

    {

        if(ind[i]==0 and vis[i]==0)

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

    }

    cout<<ans;

    return  0 ;

}


// Faster C++ solution for the same problem (measured speedup: 3.899527104152032x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 4.337073454530132x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#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;

}//z

// Faster C++ solution for the same problem (measured speedup: 4.337073454530132x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const int N = 1e5+10 , M = 5e5+10, OO = 0x3f3f3f3f ,  MOD = 1e9+7;

const double EPS = 1e-3;

#define M_PI 3.14159265358979323846

#define clr(arr,val) memset(arr , val , sizeof (arr))

#define loop(i,n) for(int i = 0;i < int(n);i++)

#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)

#define range(i,a,b) for(int i = int(a);i <= int(b);i++)

#define pb push_back

#define F first

#define S second

#define all(v) ((v).begin()) , ((v).end())

#define sz(v)  ((int)((v).size()))

typedef pair <int,int> pii ;

typedef vector < pii > vii ;

typedef vector < int > vi;

int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

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

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





int n , m ,u, v, nxt[N],to[N],head[N],ne;

int dp[N];



void init(){



    ne = 0;

    memset(head, -1 , sizeof head);

}



void addEdge(int f, int t){



    to[ne] = t;

    nxt[ne] = head[f];

    head[f] = ne++;

}



int solve(int idx){



   // if(idx == n) return 0;



    int& ret = dp[idx];

    if(~ret) return ret;

    //if(~ret) return 0;



    ret = 0;

    for (int e = head[idx]; ~e ; e = nxt[e])

        ret = max(ret, solve(to[e])+1);





    return ret;



}







int main(){





    //ios_base::sync_with_stdio(0);

    //cin.tie(0);cout.tie(0);





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

    init();



    loop(i , m){

        scanf("%d %d",&u,&v);

        addEdge(u, v);

    }



    int mx = 0;

    memset(dp, -1 , sizeof dp);





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

        mx = max(mx , solve(i));





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



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.7716532136265344x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#include <bits/stdc++.h>





using namespace std;



vector<int> v[100005];

bool processado[100005];

int tab[100005];



int dfs(int s)

{



    //processado[s] = true;

    int soma, max = 0, entrou = 0;

    

    if(tab[s] >= 0)

    {

        return tab[s];

    }

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

    {



        entrou = 1;

        soma = dfs(v[s][i]) + 1;

        if(soma > max)

        {

            max = soma;

            tab[s] = max;

        }

    }

    if(entrou == 1)

    {

        return max;

    }



    return tab[s] = 0;



}

 





int main(){



    int n, m;

    cin >> n >> m;

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



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

    {

        processado[i] = false;

    }*/



    int x, y;

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

    {

        cin >> x >> y;

        v[x].push_back(y);

    }



    int soma = 0;

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

    {

        soma = max(dfs(i), soma);

    }



    cout << soma << endl;











    



    

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.893509966341837x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
# include <bits/stdc++.h>



using namespace std;



vector < int > adj[100007];



int used[100007], dp[100007], mx;



void dfs( int v ) {

	if ( used[v] ) return;

	used[v] = 1;

	for ( auto u: adj[v] ) {

		dfs(u);

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

	}

}



int main() {

	int n, m;

	cin >> n >> m;

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

		int u, v;

		cin >> u >> v;

		adj[u].push_back(v);

	}

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

		dfs(i);

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

	}

	cout << mx << endl;

}

// Faster C++ solution for the same problem (measured speedup: 3.5022577486226742x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 100;

int n, m, mem[N];

bool vis[N];

vector<int> adjList[N];



int dp(int v) {

	if (vis[v]) {

		return mem[v];

	}

	vis[v] = true;



	int ans = 0;

	for(int u : adjList[v]) {

		ans = max(ans, dp(u) + 1);

	}



	return mem[v] = ans;

}



int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);



	cin >> n >> m;



	int a, b;

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

		cin >> a >> b;

		adjList[a].emplace_back(b);

	}



	int ans = 0;

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

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

	}



	cout << ans << '\n';



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.522831078951028x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
/**

     **author : _KMS_

     **created : 30-03-2020

**/

#include<bits/stdc++.h>

using namespace std;

const long long M =3e5+10,MOD=1e9+7;

typedef long long ll;

vector<int>v[M];

bool vis[M];

int dp[M];

void dfs(int i)

{

  vis[i]=1;

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

  {

    int y=v[i][j];

    if(!vis[y])dfs(y);

    dp[i]=max(dp[i],dp[y]+1);

  }

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

     int n,m;

     cin>>n>>m;

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

     {

       int x,y;

       cin>>x>>y;

       v[x].push_back(y);

     }

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

     {

      if(!vis[i])

      {

        dfs(i);

      }

     }

    sort(dp+1,dp+n+1);

    cout<<dp[n]<<"\n";

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 9.354463853579464x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <algorithm>

#include<cmath>

#include <vector>

typedef long long ll;

using namespace std;

int dp[100005] , vis[100005];

vector<int>graph[100005];

void dfs(int node)

{

    vis[node] = 1;

    for (auto child : graph[node])

    {

        if (!vis [child])

            dfs(child);

        dp[node] = max(dp[child] + 1, dp[node]);

    }

}

int main()

{

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, e,ans=0,u,v;

    cin >> n >> e;

    while (e--)

    {

        cin >> u >> v;

        graph[u].push_back(v);

    }

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

    {

        if (!vis[i])

            dfs(i);

    }

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

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

    cout << ans;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.521469339145882x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

using namespace std;

const int maxn=100000+10;

struct node

{

	int to,next,w;

}edge[maxn];

int k=1,head[maxn],ind[maxn],n,m,dp[maxn];

void add(int u,int v,int w)

{

	edge[++k].to=v;edge[k].next=head[u];head[u]=k;edge[k].w=w;

}

void Topsort()

{

	queue<int>q;

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

		if(!ind[i])

			q.push(i);

	while(!q.empty())

	{

		int x=q.front();q.pop();

		for(int i=head[x];i;i=edge[i].next)

		{

			dp[edge[i].to]=max(dp[edge[i].to],dp[x]+edge[i].w);

			ind[edge[i].to]--;

			if(!ind[edge[i].to])

				q.push(edge[i].to);

		}

	}

}

int  main()

{

	cin>>n>>m;

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

	{

		int u,v;

		cin>>u>>v;

		add(u,v,1);

		ind[v]++;

	}

	Topsort();

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

//Phoenix-ZH