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

using namespace std;

const int maxn=100010;

int n,m,ans;

vector<int> edge[maxn];

int in_dgr[maxn],dp[maxn];

queue<int> q;

int main()

{

	cin>>n>>m;

	while(m--)

	{

		int u,v;

		cin>>u>>v;

		edge[u].push_back(v);

		in_dgr[v]++;

	}

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

		if(!in_dgr[i])

			q.push(i);

	while(!q.empty())

	{

		int now=q.front();

		q.pop();

		for(int i=0;i<edge[now].size();i++)

		{

			int to=edge[now][i];

			dp[to]=max(dp[to],dp[now]+1);

			if(--in_dgr[to]==0)

				q.push(to);

			ans=dp[to];

		}

	}

	printf("%d",ans);

	return 0;

}

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



using namespace std;



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

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

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
/* long long or int */

/* inline & usighed & sighned main */

/* xx 396 */



#include<bits/stdc++.h>

using namespace std;



const int MAX_N=1e5+10;



stack <int> S;

int N,M,deg[MAX_N],dist[MAX_N];

vector <int> G[MAX_N];



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) S.push(i);

	while(!S.empty()){

		int fr=S.top(); S.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) S.push(to);

		}

	}

	cout<<(*max_element(dist,dist+N))<<endl;

	return 0;

}

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



using namespace std;



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

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

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

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

using namespace std;



int N, M, a, b, ans;

vector<int> V[100100];

int dp[100100];



int f(int a) {

	if (V[a].size() == 0) return 0;

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

	dp[a] = 0;

	for(int i=0; i<V[a].size(); i++) {

		int b = V[a][i];

		dp[a] = max(dp[a], f(b)+1);

	}

	return dp[a];

}



int main() {

	cin >> N >> M;

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

		cin >> a >> b;

		V[a].push_back(b);

	}

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

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

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

	}

	cout << ans << endl;

	return 0;

}

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



using namespace std;



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

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

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

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

using namespace std;

#define N (int)2e5

#define fi first

#define se second

#define mp make_pair

#define pb push_back

int n,m,memo[N];

vector <int> adj[N];



int dp(int u) {

    if (adj[u].size() == 0) return 0;

    int &sol = memo[u];

    if (sol != -1) return sol;

    int keep = INT_MIN;

    for (int v : adj[u]) {

        keep = max(

            keep ,

            dp(v) + 1

        );

    }

    return sol = keep;

}



int main() {

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

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

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

        adj[u].pb(v);

    }

    memset (memo , -1 , sizeof memo);

    int sol = INT_MIN;

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

        sol = max(sol , dp(u));

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

}


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



using namespace std;



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

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

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
#include<queue>

#include<cstdio>

using namespace std;

const int maxn = 1e5 + 5;



queue<int>que;

vector<int>vec[maxn];

int in[maxn],ans[maxn];



int main(){

	int n, m;

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

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0){

			que.push(i);

			ans[i] = 0;

		}

	}

	int now;

	while(!que.empty()){

		now = que.front();que.pop();

		int len = vec[now].size();

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

			in[vec[now][i]]--;

			if(in[vec[now][i]] == 0){

				que.push(vec[now][i]);

				ans[vec[now][i]] = ans[now] + 1;

			}

		}

	}

	int cnt = 0;

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

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

}

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



using namespace std;



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

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

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<queue>

#include<cstring>

#include<vector>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



vector<int>vec[maxn];

queue<int>q;

int in[maxn],num[maxn];



int main(){	

	int n,m;

	scanf("%d",&n);

	scanf("%d",&m);

	for(int i = 0; i <= n; i++) in[i] = 0;

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0) q.push(i);

	}

	int ans = 0;

	while(!q.empty()){

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

		int len = vec[now].size();

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

			if(--in[vec[now][i]] == 0){

				q.push(vec[now][i]);

				num[vec[now][i]] = num[now] + 1;

				ans = max(ans, num[vec[now][i]]);

			}

		}

	}

	printf("%d",ans);

}

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



using namespace std;



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

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

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<queue>

#include<vector>

using namespace std;

const int N=1e5+10;

vector<int>f[N],a;

queue<int>q;

int n,m,p,x,y,in[N],ans,dp[N];

int main()

{

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

	while(m--)

	{

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

		f[x].push_back(y);

		in[y]++;

	}

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

		if(!in[i])q.push(i);

	while(!q.empty())

	{

		p=q.front();

		q.pop();

		a.push_back(p);

		for(int j=0;j<f[p].size();j++)

			if(!--in[f[p][j]])q.push(f[p][j]);

	}

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

	{

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

			dp[f[a[i]][j]]=dp[a[i]]+1;

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

	}

	printf("%d",ans);

	return 0;

}

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

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

int dis[maxn], dep[maxn], pre[maxn][21];

int n,m,ans;

int d[maxn],c[maxn],dp[maxn];

void init()

{

    ms(head, -1);

    tot = 0;ms(d,0);ans=0;ms(dp,0);

}

void add(int x, int y, int val)

{

    e[tot].to = y, e[tot].val = val, e[tot].next = head[x], head[x] = tot++;

}



int dfs(int x)

{

	if(!c[x]) return 0;

	if(dp[x])return dp[x];

	ll tt=0;

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

    {

    	int y=e[i].to;

        tt=max(tt,dfs(y)+1);

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

		if(!d[y])d[y]=1;//d为出度 

		if(!c[x])c[x]=1;

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

		//cout<<dp[i]<<endl;

	}

	cout<<ans<<endl;

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

using namespace std;

const int maxn=100010;

int n,m,ans;

vector<int> edge[maxn];

int in_dgr[maxn],dp[maxn];

queue<int> q;

int main()

{

	cin>>n>>m;

	while(m--)

	{

		int u,v;

		cin>>u>>v;

		edge[u].push_back(v);

		in_dgr[v]++;

	}

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

		if(!in_dgr[i])

			q.push(i);

	while(!q.empty())

	{

		int now=q.front();

		q.pop();

		for(int i=0;i<edge[now].size();i++)

		{

			int to=edge[now][i];

			dp[to]=max(dp[to],dp[now]+1);

			if(--in_dgr[to]==0)

				q.push(to);

			ans=dp[to];

		}

	}

	printf("%d",ans);

	return 0;

}

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

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

int dis[maxn], dep[maxn], pre[maxn][21];

int n,m,ans;

int d[maxn],c[maxn],dp[maxn];

void init()

{

    ms(head, -1);

    tot = 0;ms(d,0);ans=0;ms(dp,0);

}

void add(int x, int y, int val)

{

    e[tot].to = y, e[tot].val = val, e[tot].next = head[x], head[x] = tot++;

}



int dfs(int x)

{

	if(!c[x]) return 0;

	if(dp[x])return dp[x];

	ll tt=0;

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

    {

    	int y=e[i].to;

        tt=max(tt,dfs(y)+1);

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

		if(!d[y])d[y]=1;//d为出度 

		if(!c[x])c[x]=1;

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

		//cout<<dp[i]<<endl;

	}

	cout<<ans<<endl;

}
// Slower C++ solution:
/* long long or int */

/* inline & usighed & sighned main */

/* xx 396 */



#include<bits/stdc++.h>

using namespace std;



const int MAX_N=1e5+10;



stack <int> S;

int N,M,deg[MAX_N],dist[MAX_N];

vector <int> G[MAX_N];



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) S.push(i);

	while(!S.empty()){

		int fr=S.top(); S.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) S.push(to);

		}

	}

	cout<<(*max_element(dist,dist+N))<<endl;

	return 0;

}

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

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

int dis[maxn], dep[maxn], pre[maxn][21];

int n,m,ans;

int d[maxn],c[maxn],dp[maxn];

void init()

{

    ms(head, -1);

    tot = 0;ms(d,0);ans=0;ms(dp,0);

}

void add(int x, int y, int val)

{

    e[tot].to = y, e[tot].val = val, e[tot].next = head[x], head[x] = tot++;

}



int dfs(int x)

{

	if(!c[x]) return 0;

	if(dp[x])return dp[x];

	ll tt=0;

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

    {

    	int y=e[i].to;

        tt=max(tt,dfs(y)+1);

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

		if(!d[y])d[y]=1;//d为出度 

		if(!c[x])c[x]=1;

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

		//cout<<dp[i]<<endl;

	}

	cout<<ans<<endl;

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

using namespace std;



int N, M, a, b, ans;

vector<int> V[100100];

int dp[100100];



int f(int a) {

	if (V[a].size() == 0) return 0;

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

	dp[a] = 0;

	for(int i=0; i<V[a].size(); i++) {

		int b = V[a][i];

		dp[a] = max(dp[a], f(b)+1);

	}

	return dp[a];

}



int main() {

	cin >> N >> M;

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

		cin >> a >> b;

		V[a].push_back(b);

	}

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

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

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

	}

	cout << ans << endl;

	return 0;

}

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

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

int dis[maxn], dep[maxn], pre[maxn][21];

int n,m,ans;

int d[maxn],c[maxn],dp[maxn];

void init()

{

    ms(head, -1);

    tot = 0;ms(d,0);ans=0;ms(dp,0);

}

void add(int x, int y, int val)

{

    e[tot].to = y, e[tot].val = val, e[tot].next = head[x], head[x] = tot++;

}



int dfs(int x)

{

	if(!c[x]) return 0;

	if(dp[x])return dp[x];

	ll tt=0;

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

    {

    	int y=e[i].to;

        tt=max(tt,dfs(y)+1);

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

		if(!d[y])d[y]=1;//d为出度 

		if(!c[x])c[x]=1;

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

		//cout<<dp[i]<<endl;

	}

	cout<<ans<<endl;

}