stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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);

}

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

}

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

}




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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

using namespace std;

#define mx 200005

vector<int>adj[mx];

bool vis[mx];

int dp[mx];



void dfs(int src){

vis[src] = 1;



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

    if(!vis[adj[src][i]])

        dfs(adj[src][i]);

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

}

}

int longest_path(int n){

    int ans = 0;

    memset(dp,0,sizeof dp);

    memset(vis,0,sizeof vis);



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

        if(!vis[i])

            dfs(i);

    }

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

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

    }

return ans;

}









int main(){

int n,m;

cin>>n>>m;

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

    int x,y;

    cin>>x>>y;

    adj[x].push_back(y);

}



cout<<longest_path(n)<<endl;

}


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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

using namespace std;

typedef long long ll;

#define fast ios_base::sync_with_stdio(false),cin.tie(NULL)

#define INF 1e18

int n, m;

vector<int> graph[100020];

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

vector<bool> visited(100020, false);

int dfs(int node, int sum)

{

    if(dp[node] != -1)

        return dp[node];

    if(graph[node].empty())

        return 1;

    else{

        int temp, max = 0;

        for (auto child : graph[node]) {

            temp = dfs(child, sum+1);

            if(temp > max)

                max = temp;

        }

        return dp[node] = max+1;

    }

}

int check()

{

    int mx = 0;

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

        if(!visited[i])

            mx = max(mx, dfs(i, 0));

    }

    return mx-1;

}

int main() {

    fast;

    cin>>n>>m;

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

        int a, b;

        cin>>a>>b;

        graph[a].push_back(b);

    }

    cout<<check();

}

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

}


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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

using namespace std;



int n, m;

vector<int> edges[100100];

bool vis[100100];

int dp[100100];



int dfs( int node ) {

  if ( vis[node] ) return dp[node];

  vis[node] = 1;

  int res = 0;

  for (auto el: edges[node])

    res = max(res, 1 + dfs(el));

  return dp[node] = res;

}



int main() {

  ios::sync_with_stdio(false);

  cin >> n >> m;



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

    int u, v;

    cin >> u >> v;

    edges[u].push_back(v);

  }



  int ans = 0;

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

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

  }



  cout << ans << endl;

  return 0;

}


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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

using namespace std;

#define f first

#define s second

#define ll long long

const int N = 1e5+10;

vector < int > graph[N] ;

int vis[N] , n , mx1  ,m;

map < int , int > mp ;

int dfs ( int node ){

    int mx = 0 ;

    for (auto i : graph[node]){

        if (!vis[i]) {

            vis[i] = 1 ;

            mp[i] = dfs(i) ;

            mx = max(mx,mp[i]+1);

        }

        else

            mx = max(mx,1+mp[i]);

    }

    mp[node] = mx ;

    return mp[node] ;

}

int main()

{

    ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );

    cin >> n >> m ;

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

        int u , v ;

        cin >> u >> v ;

        graph[u].push_back(v) ;

    }

    int mx = 0 ;

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

        if (!vis[i]){

            vis[i] = 1 ;

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

        }

    }

    cout << mx ;

}




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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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

#include <stdio.h>

#include <string>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>

#include <set> 

#include <map>

#include <queue>

#include <deque>

#include <stack>

#include <bitset>

#include <iomanip>

using namespace std;

#define assem99 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define ll long long

#define ull unsigned long long

#define ld long double

#define ii pair<int, int>

#define pll pair<ll, ll>

#define vii vector<ii>

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

#define F first

#define S second

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

#define ReadFile freopen("input.txt","r",stdin)

#define WriteFile freopen("output.txt","w",stdout)

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

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

const int MOD = 998244353, N = 2e5 + 5;

const double pi = acos(-1);



vector<int> graph[N];

bool vis[N];

int dp[N];



void dfs(int node)

{

	if (vis[node]) return;

	vis[node] = true;

	for (int child : graph[node])

	{

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

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

	}

}



int main()

{

	assem99;



	int n, m; cin >> n >> m;

	f(i, 0, m)

	{

		int u, v; cin >> u >> v;

		graph[u].push_back(v);

	}

	int ans = 0;

	f(i, 1, n + 1) if (!vis[i]) dfs(i);

	f(i, 1, n + 1) ans = max(ans, dp[i]);



	cout << ans;



	return 0;

}


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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}

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



/**

       return NO_PAIN ? NO_GAIN : GAIN;

*/



using namespace std;



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

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

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

#define endl "\n"

#define pb push_back

#define START cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

#define isOdd(n)    (n&1)

#define pow(n, m) (ll)powl(n, m)

#define numOfDigits(n)  ((n)? (ll)(log10(n))+1 : 1)

#define clr(x, val)    memset(x, val, sizeof(x)) //be careful of value and TLE

#define MOD 1000000007

#define EPS 1e-7

#define LSOne(x)  (x & (-x))   //least significant 1

typedef long long ll;

typedef long double ld;

typedef pair<int, int> pii;

typedef vector<int> vi;



const int N = 1e5 + 1;

vector<vi> adj(N);

bool vis[N];

int dp[N];

void dfs(int u){

    /*

    for each vertex, ask it what is the longest path for you.

     then his parent's longest path is max + 1

    */

    vis[u] = 1;

    for(int i = 0; i<sz(adj[u]); i++){

        int v = adj[u][i];

        if(!vis[v])

            dfs(v);

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

    }

}

int main()

{

    START

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

    //freopen("test cases\\in.txt", "w", stdout);

    int n, m; cin>>n>>m;



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

        int u, v; cin>>u>>v;

        adj[u].pb(v);

    }

    int mx = 0;

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

        if(!vis[i] && sz(adj[i]))

            dfs(i);



    cout<<*max_element(dp, dp+N);

	return 0;

}


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

using namespace std;



const int maxn=1e5+5;

struct edge{

	int to,next;

}e[maxn];



int head[maxn];

int cnt,ans;



void add(int x,int y)

{

	e[cnt].to=y;

	e[cnt].next=head[x];

	head[x]=cnt;

	cnt++;

}



int ru[maxn],chu[maxn],dp[maxn];

int dfs(int x)

{

	if(chu[x]==0) return 0;

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

	int ans=0;

	

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

	ans=max(dfs(e[i].to)+1,ans);

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

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

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

 } 

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

using namespace std;



int n, m, vis[100009], dis[100009];

vector<int> adj[100009];



int dfs(int s)

{

    if(vis[s])

        return dis[s];

    vis[s] = 1;

    int ret = 0;

    for(auto u : adj[s])

        ret = max(ret, dfs(u) + 1);



    return dis[s] = ret;

}



int main()

{

    int i;

    cin >> n >> m;



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

    {

        int x, y;

        cin >> x >> y;

        adj[x].push_back(y);

    }



    int ans = 0;

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

    {

        if(!vis[i])

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

    }



    cout << ans;

}


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

using namespace std;



const int maxn=1e5+5;

struct edge{

	int to,next;

}e[maxn];



int head[maxn];

int cnt,ans;



void add(int x,int y)

{

	e[cnt].to=y;

	e[cnt].next=head[x];

	head[x]=cnt;

	cnt++;

}



int ru[maxn],chu[maxn],dp[maxn];

int dfs(int x)

{

	if(chu[x]==0) return 0;

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

	int ans=0;

	

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

	ans=max(dfs(e[i].to)+1,ans);

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

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

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

 } 

// Fastest known solution for this problem: