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

}


// Optimized solution to the same problem (faster):
#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

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

}

// Optimized solution to the same problem (faster):
#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

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

#include <vector>

#include <map>

#include <algorithm>

#define ll long long



using namespace std;



int n, m, x, y, mx = 0;

vector<int> graph[100005];

int dp[100005];

bool vis[100005];



int dfs(int node)

{

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

	vis[node] = 1;

	for (int i = 0; i < graph[node].size(); i++)

	{

		int child = graph[node][i];

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

	}

	return dp[node];

}



int main()

{

	cin >> n >> m;

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

	{

		cin >> x >> y;

		graph[x].push_back(y);

	}

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

	{

		if (!vis[i])

		{

			int l = dfs(i);

		}

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

	}

	cout << mx << '\n';

	return 0;

}

// Optimized solution to the same problem (faster):
#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

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

using namespace std;

#define ll long long

    const int N  = 1e5 + 5 ;

    vector < int > adj [ N ] ;

        bool visited[ N ]  ;

         int deg [ N] , dis[N];



         void dfs (int node ){

                if (!visited[node])

                    visited[node] = true;



                for ( int X : adj[node]){

                    dis[X] = max(dis[X] , dis[node] +1);

                    deg[X]--;

                    if (deg[X]==0)

                        dfs(X);

                }



         }



int main () {



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



            while (m--){

                int a , b ;

                 cin >> a >> b ;

                  adj[a].push_back(b);

                  deg[b]++;



            }

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

                if (!visited[i] && deg[i] == 0)

                    dfs(i);

            }

            int answer = 0 ;

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

                answer = max (answer,dis[i]);

            }

            cout << answer<<endl;







 return 0;

}






// Optimized solution to the same problem (faster):
#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

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

}

// Optimized solution to the same problem (faster):
#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

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

#include <cassert>

#include <algorithm>

#include <vector>

#include <map>

#define N int(1e5+10)

#include <queue>

using namespace std;



vector<int> e[N];

int dp[N];

map<int,int> ind;



int main(int argc,char ** argv)

{

    int n,m,a,b;

    cin>>n>>m;

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

        cin>>a>>b;

        e[a].push_back(b);

        ++ind[b];

    }

    queue<int>q;

    for(int i=1;i<=n;++i)if(ind[i]==0)q.push(i);

    while(!q.empty()){

        int cur=q.front();

        q.pop();

        for(auto iter:e[cur]){

            if(--ind[iter]==0){

                q.push(iter);

            }

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

        }



    }

    int ans=0;

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

    cout<<ans<<endl;

    return 0;

}

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

#define int long long

#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

#define endl '\n'

#define fori(s,n) for(int i=s;i<n;i++) 

#define forj(s,n) for(int j=s;j<n;j++) 

#define vii vector<int>

int t = 1, n, m;

vector<vii> edges;

int dp[100000 + 1];

int solve(int i) {///1  2  

	if (edges[i].size() == 0) return 0;

	int& ret = dp[i];

	if (~ret) return ret;

	int r1 = 0;

	for (auto child : edges[i]) {

		r1 = max(r1, 1 + solve(child));

	}

	return ret=r1;

}

signed main()

{

	fast;

   //cin>>t;

	int u, v;

	while (t--)

	{

		memset(dp, -1, sizeof dp);

		cin >> n >> m;

		edges = vector<vii>(n + 1);

		while (m--) {

			cin >> u >> v;

			edges[u].push_back(v);

		}

		int ans = 0;

		fori(1, n + 1) {

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

		}

		cout << ans << endl;

	}

}

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

#include <set>

using namespace std;

#define mp             make_pair

#define pb             push_back

#define fi             first

#define se             second

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

#define fill(x, y)     memset(x, y, sizeof(y))

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

typedef long long      ll;

typedef pair<int, int> ii;

typedef pair<ii, int>  iii;

typedef vector<int>    vi;

typedef vector<ii>     vii;

typedef vector<iii>    viii;



int n;

int opt;

bool visited[100010];

vector<int> graph[100010];

int in[100010];

int dp[100010];



void dfs(int u) {

    visited[u] = true;

    for (int v : graph[u]) {

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

        in[v]--;

        if (in[v] == 0) dfs(v);

    }

}



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  int m;

  cin >> n >> m;

  int a, b;

  opt = 0;

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

      cin >> a >> b;

      graph[a].pb(b);

      in[b]++;

  }

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

      if (!visited[i] && in[i] == 0) dfs(i);

  }

  int opt = 0;

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

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

  }

  cout << opt << "\n";

  return 0;

}

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

#define int long long

#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

#define endl '\n'

#define fori(s,n) for(int i=s;i<n;i++) 

#define forj(s,n) for(int j=s;j<n;j++) 

#define vii vector<int>

int t = 1, n, m;

vector<vii> edges;

int dp[100000 + 1];

int solve(int i) {///1  2  

	if (edges[i].size() == 0) return 0;

	int& ret = dp[i];

	if (~ret) return ret;

	int r1 = 0;

	for (auto child : edges[i]) {

		r1 = max(r1, 1 + solve(child));

	}

	return ret=r1;

}

signed main()

{

	fast;

   //cin>>t;

	int u, v;

	while (t--)

	{

		memset(dp, -1, sizeof dp);

		cin >> n >> m;

		edges = vector<vii>(n + 1);

		while (m--) {

			cin >> u >> v;

			edges[u].push_back(v);

		}

		int ans = 0;

		fori(1, n + 1) {

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

		}

		cout << ans << endl;

	}

}

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



        dp[s]=max(dp[s],len+dp[x]);

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

        //adj[y].push_back(x);

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

}




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

#define int long long

#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

#define endl '\n'

#define fori(s,n) for(int i=s;i<n;i++) 

#define forj(s,n) for(int j=s;j<n;j++) 

#define vii vector<int>

int t = 1, n, m;

vector<vii> edges;

int dp[100000 + 1];

int solve(int i) {///1  2  

	if (edges[i].size() == 0) return 0;

	int& ret = dp[i];

	if (~ret) return ret;

	int r1 = 0;

	for (auto child : edges[i]) {

		r1 = max(r1, 1 + solve(child));

	}

	return ret=r1;

}

signed main()

{

	fast;

   //cin>>t;

	int u, v;

	while (t--)

	{

		memset(dp, -1, sizeof dp);

		cin >> n >> m;

		edges = vector<vii>(n + 1);

		while (m--) {

			cin >> u >> v;

			edges[u].push_back(v);

		}

		int ans = 0;

		fori(1, n + 1) {

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

		}

		cout << ans << endl;

	}

}

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

using namespace std;



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

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

#define fn(i,a,b) for(int i=a ; i>=b ; i--)

#define pb push_back

#define mk make_pair

#define ff first

#define ss second

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



typedef long long ll;

typedef pair<int,int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

typedef vector<ll> vll;



const int M=1e5+5;

const ll INF=1e15;

const int MOD=1e9+5;



int n,m,r;

int dp[M];

vi vec[M];



int DP(int x){

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

	dp[x] = 0;

	fp(i,0,vec[x].size()) dp[x] = max(dp[x] , DP(vec[x][i]) + 1);

	return dp[x];

}



int main(){



	fastio;

	cin >> n >> m;

	fp(i,1,n+1) dp[i] = -1;



	while (m--){

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

		vec[x].pb(y);

	}



	fp(i,1,n+1)

		if (dp[i] == -1) r = max(r,DP(i));



	cout << r << "\n";



	return 0;

}


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

#define int long long

#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

#define endl '\n'

#define fori(s,n) for(int i=s;i<n;i++) 

#define forj(s,n) for(int j=s;j<n;j++) 

#define vii vector<int>

int t = 1, n, m;

vector<vii> edges;

int dp[100000 + 1];

int solve(int i) {///1  2  

	if (edges[i].size() == 0) return 0;

	int& ret = dp[i];

	if (~ret) return ret;

	int r1 = 0;

	for (auto child : edges[i]) {

		r1 = max(r1, 1 + solve(child));

	}

	return ret=r1;

}

signed main()

{

	fast;

   //cin>>t;

	int u, v;

	while (t--)

	{

		memset(dp, -1, sizeof dp);

		cin >> n >> m;

		edges = vector<vii>(n + 1);

		while (m--) {

			cin >> u >> v;

			edges[u].push_back(v);

		}

		int ans = 0;

		fori(1, n + 1) {

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

		}

		cout << ans << endl;

	}

}

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

#include<iomanip>

#include<algorithm>

#include<cstring>

#include<string>

#include<cmath>

#include<vector>

#include<queue>

#include<stack>

#include<map>

#include<set>

using namespace std;

const int inf=2147483647,dx[]={-1,0,1,0},dy[]={0,-1,0,1};// 上 左 下 右

const int N=100005,M=1000005,mod=1000000007;

const long long llinf=9223372036854775807ll;

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

vector<int> v[N];

bool notroot[N];

int dfs(int x)

{

	if(dp[x]!=-1)

		return dp[x];

	if(v[x].empty())

	{

		dp[x]=0;

		return 0;

	}

	int res=-666666;

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

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

	dp[x]=res;

	return res;

}

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);

		notroot[y]=true;

	}

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

		if(!notroot[i])

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

	cout<<ans;

	return 0;

}

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

#include<iomanip>

#include<algorithm>

#include<cstring>

#include<string>

#include<cmath>

#include<vector>

#include<queue>

#include<stack>

#include<map>

#include<set>

using namespace std;

const int inf=2147483647,dx[]={-1,0,1,0},dy[]={0,-1,0,1};// 上 左 下 右

const int N=100005,M=1000005,mod=1000000007;

const long long llinf=9223372036854775807ll;

int n,m,ai,bi,ans,q[N],head,tail,dp[N],a[N];

vector<int> v[N];

int main(){

	cin>>n>>m;

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

	{

		cin>>ai>>bi;

		v[ai].push_back(bi);

		a[bi]++;

	}

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

		if(!a[i])

			q[++tail]=i;

	while(head<tail)

	{

		int x=q[++head];

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

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

		{

			int y=v[x][i];

			a[y]--;

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

			if(!a[y])

				q[++tail]=y;

		}

	}

	cout<<ans;

	return 0;

}

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

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )

#define rep(i,n) REP(i,0,n)

#define F first

#define S second

 

const int mx=200010;

const ll mod=1e9+7;



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

vector<int> v[mx];



int rec(int x){

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

  dp[x]=0;

  for(auto to:v[x]){

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

  }

  return dp[x];

}





int main(){  

  int n,m;

  cin >> n >> m;



  rep(i,m){

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

    v[x].emplace_back(y);

  }

  

  int ans=0;

  rep(i,n){ ans = max(ans,rec(i)); }

  cout << ans << endl;

  return 0;

}

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

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )

#define rep(i,n) REP(i,0,n)

#define F first

#define S second

constexpr char ln =  '\n';



const int mx=100010;

const ll mod=1e9+7;



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

vector<int> v[mx];



int rec(int x){

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

  int tmp = 0;

  for(auto it:v[x]){

    tmp = max(tmp, rec(it)+1);

  }

  return dp[x] = tmp; 

}



int main(){

  int n,m;

  cin >> n >> m;

  

  rep(i,m){

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

    v[x].emplace_back(y);

  }

  

  int ans = 0;

  rep(i,n){

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

  }

  cout << ans << ln;

  return 0;

}

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

using namespace std;

const int MAXN=1e5+100;

int dp[MAXN];

vector<int>E[MAXN];

int dfs(int t)

{

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

	if(E[t].empty()) return dp[t]=0;

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

		dp[t]=max(dp[t],dfs(E[t][i])+1);

	return dp[t];

}

int main()

{

	int n,m; 

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

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

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

	{

		int x,y;

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

		E[x].push_back(y);

	}

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

		dfs(i);

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

	return 0;

}

/*

#include<bits/stdc++.h>

using namespace std;

const int N=1e5+10;

queue<int>q;

vector<int>E[N];

int n,m,ans;

int dp[N],indegree[N];

int main()

{

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

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

	{

		int x,y;

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

		E[x].push_back(y); 

		indegree[y]++;

	}

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

		if(!indegree[i]) 

			q.push(i);

    while(!q.empty()) 

	{

    	int u=q.front(); 

		q.pop();

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

		{

			int uu=E[u][i];

			indegree[uu]--;

			if(!indegree[uu]) 

			{

				dp[uu]=dp[u]+1;

				q.push(uu);

			}

		}

	}

	int maxn=0;

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

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

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

	return 0;

}

*/

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

using namespace std;

int n,m,a,b,dp[100010],to[100010];

vector <int> v[100010];

queue <int> q;



int main()

{

	cin>>n>>m;

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

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

		v[a].push_back(b);

		to[b]++;

	}	

	for(int i=1;i<=n;++i)if(to[i]==0)q.push(i);

	while(!q.empty()){

		int x=q.front();

		q.pop();

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

			if(dp[x]+1>dp[v[x][i]]){

				dp[v[x][i]]=dp[x]+1;

			}

			to[v[x][i]]--;

			if(to[v[x][i]]==0)q.push(v[x][i]);

		}

	}

	int ans=0;

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

	cout<<ans<<endl;

	return 0;

}

// Fastest known solution for this problem: