stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



using ll = long long;

const int MAXN  = 2e5+10;

const int MAXM  = 1e5+10;



int N, M;



struct edge {

    int to, w;

};

vector<edge> G[MAXN];

int in[MAXN];

int d[MAXN];



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

    G[u].push_back({v, w});

}



int dp[MAXN];

int dfs(int s) {

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

    for(auto e : G[s]) {

        dp[s] = max(dp[s], dfs(e.to) + 1); 

    }

    return dp[s];

}

void solve() {

    int ans = 0;

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

        if(in[v] == 0){

            dp[v] = 0;

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

        } 

    }

    cout << ans << endl;

}



int main() {

    cin >> N >> M;

    int a, b;

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

        cin >> a >> b;

        in[b]++;

        add(a, b, 1);

    }

    /*

    // TLE

    int ans = 0;

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

        if(in[i] == 0) {

            d[i] = 0;

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

        }

    }

    cout << ans << endl;

    */

    

    /*

    // bfs

    queue<int> que;

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

        if(in[i] == 0) {

            d[i] = 0;

            que.push(i);

        }

    }

    while(!que.empty()) {

        int u = que.front(); que.pop();

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

            int v = G[u][i].to;

            --in[v];

            if(in[v]==0) {

                if(d[v] < d[u]+1) {

                    d[v] = d[u] + 1;

                }

                que.push(v);

            }

        }

    }



    int ans = 0;

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

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

    }    

    cout << ans << endl;

    */

   // dfs

   solve();



    return 0;

}

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

using namespace std;



using LL = long long;

const int MAXN = 100100;



int N, M;

vector<int> g[MAXN];

int ans;

int dp[MAXN];

int dfs(int v) {

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

    for(int u=0; u<g[v].size(); u++) {

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

    }

    return dp[v];

}

int main() {

    scanf("%d%d", &N, &M);

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

        int x, y;

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

        g[x].push_back(y);

    }

    

    memset(dp, 0, sizeof(dp));

    ans = 0;

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

        ans = max(ans, dfs(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<int> pred[100010];

int f[100010];

int lp(int x){

	if(f[x]!=-1)

	   return f[x];

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

		f[x]=max(f[x],lp(pred[x][i])+1);

	}

	f[x]=max(f[x],0);

	return f[x];

}

int main()

{

	cin>>n>>m;

	int a,b;

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

		cin>>a>>b;

		pred[b].push_back(a);

	}

	int maxx=0;

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

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

		if(f[i]==-1)

	      f[i]=lp(i);

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

	}

	cout<<maxx<<endl;

	return 0;

}


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

using namespace std;

queue<int> q;

int a,b;

vector<int> ga[100010];

int indeg[100010];

int n,m;

int dp[100010];

int res;

int main()

{

	cin>>n>>m;

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

		cin>>a>>b;

		ga[a].push_back(b);

		indeg[b]++;

	}

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

		if(indeg[i]==0)

		   q.push(i);

	}

	while(!q.empty()){

		int u=q.front();

		q.pop();

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

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

			int v=ga[u][i];

			indeg[v]--;

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

			if(indeg[v]==0)

			   q.push(v);

		}

	}

	cout<<res<<endl;

	return 0;

}


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

using namespace std;

int n,m;

int dp[100010];

vector<int> adj[100010];

int rec(int x){

	if(dp[x]>=0)

	   return dp[x];

	if(adj[x].empty())

	   return dp[x]=0;

	dp[x]=dp[adj[x][0]]+1;

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

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

	}

	return  dp[x];

}

int a,b;

int main()

{

	cin>>n>>m;

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

	{

		cin>>a>>b;

		adj[a].push_back(b);

	}

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

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}


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

using namespace std;

queue<int> q;

int a,b;

vector<int> ga[100010];

int indeg[100010];

int n,m;

int dp[100010];

int res;

int main()

{

	cin>>n>>m;

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

		cin>>a>>b;

		ga[a].push_back(b);

		indeg[b]++;

	}

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

		if(indeg[i]==0)

		   q.push(i);

	}

	while(!q.empty()){

		int u=q.front();

		q.pop();

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

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

			int v=ga[u][i];

			indeg[v]--;

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

			if(indeg[v]==0)

			   q.push(v);

		}

	}

	cout<<res<<endl;

	return 0;

}


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

using namespace std;

int n,m;

int dp[100010];

vector<int> radj[100010];

int rec(int x){

	if(dp[x]>=0)

	   return dp[x];

	if(radj[x].empty())

	   return dp[x]=0;

	dp[x]=dp[radj[x][0]]+1;

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

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

	}

	return  dp[x];

}

int a,b;

int main()

{

	cin>>n>>m;

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

	{

		cin>>a>>b;

		radj[a].push_back(b);

	}

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

	int ans=0;

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

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

	cout<<ans<<endl;

	return 0;

}

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

using namespace std;

queue<int> q;

int a,b;

vector<int> ga[100010];

int indeg[100010];

int n,m;

int dp[100010];

int res;

int main()

{

	cin>>n>>m;

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

		cin>>a>>b;

		ga[a].push_back(b);

		indeg[b]++;

	}

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

		if(indeg[i]==0)

		   q.push(i);

	}

	while(!q.empty()){

		int u=q.front();

		q.pop();

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

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

			int v=ga[u][i];

			indeg[v]--;

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

			if(indeg[v]==0)

			   q.push(v);

		}

	}

	cout<<res<<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> ga[100010];

int x,y;

int dp[100010];

int dypr(int u){

	if(dp[u]!=-1)

	   return dp[u];

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

		int v=ga[u][i];

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

	}

	return dp[u];

}

int main()

{

	cin>>n>>m;

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

	{

		cin>>x>>y;

		ga[y].push_back(x);

	}

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

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

	   dp[i]=dypr(i);

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

	return 0;

}


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

using namespace std;

queue<int> q;

int a,b;

vector<int> ga[100010];

int indeg[100010];

int n,m;

int dp[100010];

int res;

int main()

{

	cin>>n>>m;

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

		cin>>a>>b;

		ga[a].push_back(b);

		indeg[b]++;

	}

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

		if(indeg[i]==0)

		   q.push(i);

	}

	while(!q.empty()){

		int u=q.front();

		q.pop();

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

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

			int v=ga[u][i];

			indeg[v]--;

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

			if(indeg[v]==0)

			   q.push(v);

		}

	}

	cout<<res<<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> ga[100010];

int x,y;

int dp[100010];

int sol(int u){

	if(~dp[u])

	   return dp[u];

	if(ga[u].empty())

	   return dp[u]=0;

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

		int v=ga[u][i];

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

	}

	return dp[u];

}

int main()

{

	cin>>n>>m;

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

	{

		cin>>x>>y;

		ga[x].push_back(y);

	}

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

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

	   sol(i);

	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>

using namespace std;

queue<int> q;

int a,b;

vector<int> ga[100010];

int indeg[100010];

int n,m;

int dp[100010];

int res;

int main()

{

	cin>>n>>m;

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

		cin>>a>>b;

		ga[a].push_back(b);

		indeg[b]++;

	}

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

		if(indeg[i]==0)

		   q.push(i);

	}

	while(!q.empty()){

		int u=q.front();

		q.pop();

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

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

			int v=ga[u][i];

			indeg[v]--;

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

			if(indeg[v]==0)

			   q.push(v);

		}

	}

	cout<<res<<endl;

	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 dp[100100];

int n,m;

vector<int> ga[100100];

int u,v;

int d(int x){

	if(dp[x]!=-1)

		return dp[x];

	int &res=dp[x];

	res=0;

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

		int y=ga[x][i];

		res=max(res,d(y)+1);

	}

	return res;

}

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

	}

	int res=0;

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

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

		res=max(res,d(i));

	}

	cout<<res<<endl;

	return 0;

}


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

	}

	queue<int> q;

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

		if(deg[i]==0)

			q.push(i);

	while(q.size()){

		int c=q.front();

		q.pop();

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

			int d=ga[c][i];

			deg[d]--;

			dp[d]=max(dp[d],dp[c]+1);

			if(deg[d]==0)

				q.push(d);

		}

	}

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

	return 0;

}


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



using namespace std;

int dp[100005];

vector<int>v[100005];

int dfs(int x){

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

	if(v[x].size()==0)return dp[x]=0;

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

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

	}

	return dp[x];

}

int main(){

	int n,m;

	cin>>n>>m;

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

		int a,b;

		cin>>a>>b;

		v[a].push_back(b);

	}

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

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

	return 0;

}

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



using namespace std;

#define N_ 101000

int Deg[N_], Q[N_], head, tail, n, m, D[N_];

vector<int>E[N_];

int main() {

	int i, a, b;

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

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

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

		E[a].push_back(b);

		Deg[b]++;

	}

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

		if (!Deg[i])Q[++tail] = i;

	}

	int res = 0;

	while (head < tail) {

		int x = Q[++head];

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

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

			Deg[E[x][y]]--;

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

			if (!Deg[E[x][y]])Q[++tail] = E[x][y];

		}

	}

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

}

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

using namespace std;

typedef long long ll;

typedef long double ld;

typedef pair<int,int> P;

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

#define FOR(i, a, b) for (ll i = a; i < b; i++)

#define is(a, b) a == b

#define len(v) ll(v.size())



const ll mod=1e9+7;



//vector書き出し

template <class T>

void cout_vec(const vector<T> &vec1){

  rep(i,len(vec1)){

    cout<<vec1[i]<<' ';

  }

  cout<<'\n';

}



int n,m;

vector<int> g[100010];

vector<bool> used(100010,false);

vector<int> vi(100010);



void dfs(int v){

    if(used[v]){

        return;

    }

    used[v]=true;

    for(auto itr:g[v]){

        if(!used[itr]){

            dfs(itr);

        }

    }

    vi.push_back(v);

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin>>n>>m;

    rep(i,m){

        int x,y;

        cin>>x>>y;

        g[x].push_back(y);

    }

    vector<int> dp(100010,0);

    FOR(i,1,n+1){

        if(!used[i]){

            dfs(i);

        }

    }

    int ans=0;

    for(auto itr:vi){

        for(auto itr2:g[itr]){

            dp[itr]=max(dp[itr],dp[itr2]+1);

        }

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

    }

    cout<<ans<<endl;

}

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

using namespace std;

typedef long long ll;

typedef long double ld;

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

#define FOR(i, a, b) for (ll i = a; i < b; i++)

#define len(v) ll(v.size())

#define fi first

#define se second



template <class T>

void cout_vec(const vector<T> &vec){

  for(auto itr:vec) cout<<itr<<' ';

  cout<<'\n';

}



typedef pair<ll,ll> P;

const ll mod=1e9+7;

const ll inf=1e15;



int n,m;

vector<vector<int>> g(100010);

vector<bool> used(100010,false);

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



void dfs(int v){

  if(used[v]) return ;

  used[v]=true;

  if(g[v].empty()) dp[v]=0;

  for(auto itr:g[v]){

    dfs(itr);

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

  }

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin>>n>>m;

    rep(i,m){

      int x,y;

      cin>>x>>y;

      g[x].push_back(y);

    }

    FOR(i,1,n+1) if(!used[i]) dfs(i);

    cout<<*max_element(begin(dp),end(dp))<<endl;

}

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

}

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

}


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

}

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

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;

}

// 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):
/* 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;

}

// 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: