stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
/*

	Author: Luis Manuel D�az Bar�n (LUISMO)

	Problem:

	Online Judge:

	Idea:

*/

#include<bits/stdc++.h>

// Types

#define ll long long

#define ull unsigned long long

// IO

#define sf scanf

#define pf printf

#define mkp make_pair

#define fi first

#define se second

#define endl "\n"



using namespace std;



typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

const ll inf = 1e16 + 3;

const int mod = 1e9 + 7;

const int lim = 2e5 + 2;



int N, M;

vector<int> adj[lim];



int dp[lim];

bool mark[lim];



int DFS(int idx)

{

    if(mark[idx])

        return dp[idx];

    // for each adjacent

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

    {

        int nxt = adj[idx][i];

        int aux = DFS(nxt);

        dp[idx] = max(dp[idx], 1 + aux);

    }

    mark[idx] = true;

    return dp[idx];

}



void solve()

{

    cin >> N >> M;

    // reading edges

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

    {

        int xi, yi;

        cin >> xi >> yi;

        adj[xi].push_back(yi);

    }

    

    int answ = 0;



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

    {

        int aux = DFS(i);

        answ = max(answ, aux);

    }



    cout << answ;

}



void fastIO()

{

	cin.sync_with_stdio(false);

	cin.tie(0);

}



void IO()

{

	if(fopen("d:\\lmo.in","r") != NULL)

	{

		freopen("d:\\lmo.in","r",stdin);

	}

	else if(fopen("/media/Beijing/lmo.in","r") != NULL)

	{

		freopen("/media/Beijing/lmo.in", "r", stdin);

	}

}



int main()

{

	IO();



	fastIO();



	solve();

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define maxn 100005

int tot=0,t[maxn],nxt[maxn],hd[maxn],dp[maxn];

inline void add(int u,int v){t[++tot]=v;nxt[tot]=hd[u];hd[u]=tot;}

inline int d(int u){

	if(!dp[u]) for(int i=hd[u];i;i=nxt[i]) dp[u]=max(dp[u],d(t[i])+1);

	return dp[u];

}

int main(){

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

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

	for(int i=0;i<m;i++){scanf("%d%d",&u,&v);add(u,v);}

	for(int i=1;i<=n;i++) ans=max(ans,d(i));

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

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <climits>

#include <cstring>

#include <vector>

using namespace std;

typedef long long int ll;

const ll inf = 1000000000000;

const ll maxn = 100010;

vector <ll> adj[maxn];

ll dp[maxn];



ll dfs(ll p){

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

    ll maxpath = 0;

    for(auto x: adj[p]){

        maxpath = max(maxpath,1+dfs(x));

    }

    return dp[p] = maxpath;

}

int main(){

    memset(dp,-1,sizeof dp);



    ll n,k;



    scanf("%lld %lld",&n,&k);



    for(ll i = 0; i < k; i++){

        ll a,b;

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

        adj[a].push_back(b);

    }

    ll ans = -1;

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

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

    }

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

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
// È¥°É£¡Æ¤¿¨Çð! °ÑAC´ø»ØÀ´£¡

//      ¤Ø¡¡¡¡¡¡¡¡¡¡£¯|

// ¡¡¡¡/£Ü7¡¡¡¡¡¡ ¡Ï£ß/

// ¡¡ /¡¡©¦¡¡¡¡ £¯¡¡£¯								AC		AC		AC		AC

//    ©¦¡¡Z £ß,£¼¡¡£¯¡¡¡¡ /`©c						AC		AC		AC		AC

//    ©¦¡¡¡¡¡¡¡¡¡¡©c¡¡¡¡ /¡¡¡¡¡µ					AC		AC		AC		AC

// ¡¡Y¡¡¡¡¡¡¡¡¡¡`¡¡ /¡¡¡¡/  /						AC		AC		AC		AC

// ¡¡Øé¡ñ¡¡.¡¡¡ñ¡¡¡¡¡´¡¡/¡¡/						AC		AC		AC		AC

// ¡¡()¡¡ ¤Ø¡¡¡¡¡¡¡¡|¡¡£Ü¡´							AC		AC		AC		AC

// ¡¡¡¡>- ._¡¡ ¥£¡¡ ©¦ £¯£¯							AC		AC		AC		AC

// ¡¡ / ¤Ø¡¡¡¡ /¡¡/£¼| £Ü£Ü							AC		AC		AC		AC

// ¡¡ ©c_/¡¡¡¡(_£¯¡¡ ©¦£¯£¯							AC		AC		AC		AC

// ¡¡¡¡ 7¡¡¡¡¡¡¡¡¡¡¡¡¡¡|£¯							AC		AC		AC		AC

// ¡¡¡¡ £¾¨Dr£þ£þ`-¨D£ß/							AC		AC		AC		AC

//**************************************Accepted*****************************************//



#include<bits/stdc++.h>



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

#define pb push_back

#define mp make_pair

#define Unique(x) x.erase(unique(all(x)),x.end());



#define CIN_FILE "input.txt"

#define COUT_FILE "output.txt"



using namespace std;



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

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



int Read()

{

	char c;

	c=getchar();



	int tsqua=0;



	while(c!=' '&&c!='\n')

	{

		tsqua=(((tsqua<<1)<<1)<<1)+(tsqua<<1)+(int)(c-'0');



		c=getchar();

	}



	return tsqua;

}



typedef long long ll;



int n,m;

int Deg[111111],Q[111111];

vector<int> E[111111];

int Head,Tail;

int res;

int D[111111];



int main(int argc, char const *argv[])

{

	ios::sync_with_stdio(false);

	

	cin>>n>>m;

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

	{

		int a,b;

		cin>>a>>b;

		

		E[a].pb(b);

		Deg[b]++;

	}

	

	for(int i=1;i<=n;i++)if(!Deg[i])Q[++Tail]=i;

	

	while(Head<Tail)

	{

		int x=Q[++Head];

		

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

		

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

		

		for(int i=0;i<E[x].size();i++)D[E[x][i]]=max(D[E[x][i]],D[x]+1);

		

		for(int i=0;i<E[x].size();i++)if(!Deg[E[x][i]])Q[++Tail]=E[x][i];

	}

	

	cout<<res;

	

	//printf("Time used = %.12f",(double)(clock())/CLOCKS_PER_SEC);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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]++;

	}

	stack<int> s;

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

		if(deg[i]==0){

			s.push(i);

		} 

	}

	while(s.size()){

		int x=s.top();

		s.pop();

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

			int y=ga[x][i];

			deg[y]--;

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

			if(deg[y]==0){

				s.push(y);

			}

		} 

	}

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

	return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<set>

#include<map>

#include<cmath>

#include<ctime>

#include<queue>

#include<stack>

#include<cstdio>

#include<string>

#include<vector>

#include<random>

#include<cstdlib>

#include<cstring>

#include<iomanip>

#include<iostream>

#include<algorithm>

#include<unordered_map>

#define endl "\n"

#define fi first

#define se second

#define db double

#define gcd __gcd

#define pb push_back

#define mp make_pair

#define lowbit(x) (x & (-x))

#define PII  pair<int, int> 

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

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

#define rep(i, a, b) for(__typeof(b) i = a; i <= (b); i++)

#define Rep(i, a, b) for(__typeof(a) i = a; i >= (b); i--)

#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

template<class T> inline T qmin(T a, T b) { return a < b ? a : b; }

template<class T> inline T qmax(T a, T b) { return a > b ? a : b; }

typedef long long ll;

typedef unsigned long long ull;

const db eps = 1e-9;

const db PI = acos(-1);

const int inf = 0x3f3f3f3f;

const int mod = (int)1e9 + 7;

const int maxn = (int)1e5 + 5;//remember to modify it, No RE or MLE

const ll INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

ll qpow(ll x, ll y) { ll ret = 1; for( ; y; y >>= 1, x = (x * x) % mod) if(y & 1) ret = (ret * x) % mod; return ret; }



inline int read(){

	char ch = getchar();

	int x = 0, f = 1;

	while(ch < '0' || ch > '9'){

		if(ch == '-') f = -1;

		ch = getchar();

	}

	while('0' <= ch && ch <= '9'){

		x = x * 10 + ch - '0';

		ch = getchar();

	}

	return x * f;

}



int n, m;

vector<int> G[maxn];

int dp[maxn], ind[maxn];



void dfs(int u){

	for(int v : G[u]){

		if(!dp[v]){

			dfs(v);

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

		} else {

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

		}

	}

}



int main()

{

	n = read(), m = read();

	rep(i, 1, m) {

		int u = read(), v = read();

		G[u].pb(v);

		ind[v]++;

	}

	rep(i, 1, n){

		if(!ind[i]){

			dfs(i);

		}

	}

	printf("%d\n", *max_element(dp, dp + n + 1));

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<queue>

#include<vector>

#define ii pair<int,int>

#define iii pair<int,ii> 

using namespace std;

int v,e,a,b;

vector<int> adjl[110000];

int vis[110000];

int memo[110000];

int start = 0;

int dp(int nodes){

//	cout<<nodes<<"\n";

	int sz = adjl[nodes].size();

	if(sz == 0) return 0;

	if(memo[nodes]!=-1) return memo[nodes];

	int ret = 0;

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

//		cout<<" visit "<<adjl[nodes][i]<<"\n";

		ret = max(ret, dp(adjl[nodes][i]) + 1);

	}

//	cout<<"\n";

	return memo[nodes] = ret;

}





int main(){

	cin>>v>>e;

	for(int i = 1;i<=v;i++) adjl[0].push_back(i);

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

		cin>>a>>b;

		adjl[a].push_back(b);

	}

	int ans = 0;

	for(int i = 0;i<=v;i++) memo[i] = -1;

	ans = dp(0) -1 ;

	cout<<ans<<"\n";

//	for(int i = 0;i<=v;i++) cout<<memo[i]<<" ";cout<<"\n";

//	for(int i = 0;i<=v;i++) ans = max(ans,)

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, m, dp[N];

vector<int> gg[N];

int dfs(int v){

	if (gg[v].size() == 0 || dp[v] != 0){

		return dp[v];

	}

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

		int child = gg[v][i];

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

	}

	return dp[v];

}

int main(){

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

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

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	// cout << setprecision(10) << fixed;

	cin>>n>>m;

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

		int from, to;

		cin>>from>>to;

		gg[from].push_back(to);

	}

	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;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <queue>



using namespace std;

struct snod

{

    vector <int> v;

    int len,leg;

} nod[100005];

int n,m,i,x,y,maxi;

queue <int> q;

int main()

{

    cin>>n>>m;

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

    {

        cin>>x>>y;

        nod[x].v.push_back(y);

        nod[y].leg++;

    }

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

        if(!nod[i].leg)

        {

            q.push(i);

        }



    while(!q.empty())

    {

        int poz=q.front();

        for(int it : nod[poz].v)

        {

            nod[it].len=max(nod[it].len, nod[poz].len+1);

            nod[it].leg--;

            if(!nod[it].leg)

                q.push(it);

        }

        q.pop();

    }

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

    {

        maxi=max(maxi, nod[i].len);

    }

    cout<<maxi;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int n , m;

vector<vector<int>>vi(200005);

map<int,int>mp;

int mem[200005];

bool ok[200005];

int dfs(int node){

	if(mem[node])

		return mem[node];

	for(auto it : vi[node]){

		mem[node] = max(dfs(it) + 1 , mem[node]);

	}

	return mem[node];

}

int main() {

	memset(mem,0,sizeof(mem));

	cin>>n>>m;

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

		int x , y ; cin>>x>>y;

		vi[x].push_back(y);

		mp[y]++;

	}

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

		if(!mp[i]){

			dfs(i);

		}

	}

	int ans = 0;

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

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

	cout<<ans;

	return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <iostream>



using namespace std;



const int MAXN = 100010;



int n, m;



struct edge{

	int ne, to;

}e[MAXN];

int fir[MAXN], num = 0, d[MAXN];

inline void join(int a, int b)

{

	e[++num].ne = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN];

int q[MAXN], h = 1, t = 0;



inline void topo()

{

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

		if(!d[i]) q[++t] = i;

	while(h <= t)

	{

		int u = q[h++];

		for(int i=fir[u];i;i=e[i].ne)

		{

			int v = e[i].to;

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

			--d[v];

			if(!d[v]) q[++t] = v;

		}

	}

}



int main()

{

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

	for(int i=1,a,b;i<=m;i++)

		scanf("%d%d",&a,&b),join(a, b),d[b]++;

	topo();

	int ans = -1;

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

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

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/******

author :- Mahmoud Tarek.

*******/



#include<bits/stdc++.h>



using namespace std ;



#define rg register

#define dbg(v) cerr << #v << " " << v ;

typedef long long ll ;



const int N = 1e5 + 5 ;

vector<int> adj[N] ;

int n, m, a, b ;

int dp[N] ;



int solve(int i){



   if(i != 0 && !adj[i].size()){

     return 0 ;

   }



   int & ret = dp[i] ;

   if(~ret) return ret ;

   ret = 0 ;

   if(i == 0){

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

        ret = max(ret, solve(j)) ;

   }



   for(auto j: adj[i]){

      ret = max(ret, 1 + solve(j)) ;

   }



   return ret ;

}



int main() {

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



    cin >> n >> m ;

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

        cin >> a >> b ;

        adj[a].push_back(b) ;

    }



    memset(dp, -1, sizeof dp) ;

    cout << solve(0) ;



}





/*

  Empty set of troubles.

*/




// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f

#define INF 0x3f3f3f3f

#define pb push_back

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define ms(a, x) memset(a, x, sizeof(a))

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

#define for1(i, n) for(int i = 1; i <= n; ++i)

using namespace std;

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<long long, int> pli;

typedef pair<long long, long long> pll;

const int mod = 1e9 + 7;

const int N = 1e5+10;

const int M = 1e6+10;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

inline ll Pow(ll a, ll b) { ll ans = 1; for( ; b; b >>= 1) { if(b&1) ans = ans * a % mod; a = a * a % mod;} return ans;}

inline ll Mul ( ll a, ll b)  { ll lf = a * ( b >> 25ll ) % mod * ( 1LL << 25 ) % mod; ll rg = a * ( b & ( ( 1ll << 25 ) - 1 ) ) % mod; return ( lf + rg ) % mod;}

inline ll C(ll y, ll x) { ll ans = 1; if(y < 0 || x < 0 || y < x) return 0; y %= mod; if(!y || !x) return 1; for0(i, x) ans = ans * (y-i) % mod; for1(i, x) ans = ans * Pow(i, mod-2) % mod; return ans; }

//==================================================================================================================================================================//



int n, m, tot, head[N], nex[N], ver[N];

int dp[N], deg[N], ans;

queue<int> q;



void add(int x, int y) {

   ver[++tot] = y, nex[tot] = head[x], head[x] = tot;

}



int main() {

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

   for1(i, m) {

      int x, y;

      scanf("%d %d", &x, &y);

      add(x, y);

      deg[y]++;

   }

   for1(i, n) if(!deg[i]) q.push(i);

   while(!q.empty()) {

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

      for(int i = head[x]; i; i = nex[i]) {

         int y = ver[i]; deg[y]--;

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

         if(!deg[y]) q.push(y);

      }

   }

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

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

   return 0;

}

// Fastest known solution for this problem: