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

using namespace std;



const int N = 1e5 + 100;





#define mp make_pair

#define f first

#define s second



#define pb push_back



void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}



int n,m;



vector<int>g[N];



bool was[N];



int d[N];



vector<int>ord;

int ans;



void dfs(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs(to);

 		}

 	}

 	ord.pb(v);

}

                                                                                                          

void dfs1(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs1(to);

 		}                               

 		d[v] = max(d[v],d[to] + 1);

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

 	}                          

}





int main(){

	boost();

	cin >> n >> m;

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

	{

		int u,v;

		cin >> u >> v;

		g[u].pb(v);

	}

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

	{

		if (!was[i])

		{             

			dfs(i);

		}

	}

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

	{

		was[i] = 0;

	}

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

	{                        

		dfs1(ord[i]);            

	}

	cout << ans;       

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

/**

*   author: j o s e m a g o

*   date:

**/

using namespace std;

#pragma GCC optimize ("Ofast")

#define f first

#define s second

#define mp make_pair

#define lb lower_bound

#define ub upper_bound

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

#define mset(a,v) memset(a,v,sizeof(a))

#define pb push_back

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

#define rsz resize

#define ln "\n"



typedef double db;

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

const ld PI = 2*acos(0.0);

const int MOD = 1000000007;

const int JJJ = 100009;



int N,A;

vector<int>grafo[JJJ];

bool visited[JJJ];

int dp[JJJ];



int sol = 0;

void dfs(int nod){

    visited[nod] = true;

    dp[nod] = -1;

    for(int i = 0;i < (int)grafo[nod].size();i++){

        int xx = grafo[nod][i];

        if(!visited[xx]){

            dfs(xx);

        }

        dp[nod] = max(dp[nod],dp[xx]);

    }

    dp[nod]++;

}







int main(){



  

    ios_base::sync_with_stdio(0); cin.tie(0);





    cin >> N >> A;

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

        int a,b;

        cin >> a >> b;

        grafo[a].pb(b);

    }



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

        if(!visited[i]){

            dfs(i);

        }

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

    }



    cout<<sol;







    return 0;

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9, mod = 1e9 + 7;

const long long OO = 1e18+25;



int n, m,u,v, mx, mem[N];

vector <int> g[N];



int dp(int p)

{

    int &ret = mem[p];

    if(ret != 0) return ret;

    for(int to : g[p])

        ret = max(ret, dp(to)+1);

    return ret;

}



int main()

{

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

    while(m--)

    {

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

        g[u].push_back(v);

    }

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

    {

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

    }

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

    return 0;

}

	     		 					   	 	 								 	

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <cstdio>

#include <cstring>

#include <iomanip>

#include <string>

#include <set>

#include <vector>

#include <queue>

#include <stack>

#include <map>

using namespace std;

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

#define fore(i, a, n) for (long long i = a; i < n; i++)

#define ford(i, a, n) for (long long i = n - 1; i >= a; i--)

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

#define sl(a) scanf("%lld", &a)

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

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

#define sll(a, b) scanf("%lld%lld", &a, &b)

#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

#define debug(a) cout << a << endl

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

#define endl '\n'

#define pi acos(-1.0)

#define tr t[root]

#define lson t[root << 1]

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

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

#define ull unsigned long long

const double eps = 1e-8;

inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }

typedef long long ll;

const int inf=0x3f3f3f3f;

const int MAXN = 1e5 + 10;

int n,m;

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

ll dfs(int x)

{

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

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

	ll ans=0;

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

	{

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

				

	}//cout<<ans<<endl;

	

	return dp[x]=ans;

}

int main()

{

	sii(n,m);

	ms(head,-1);

	ll ans=0;

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

	{

		int x,y;

		sii(x,y);

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

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

	{

		if(!ru[i])

		{

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

		}

	}

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

 } 


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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

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

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

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

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



int n, m;

int dp[N], in[N];

vector<int>G[N];

queue<int>q;

int ans = 0;



int main() {

   int a, b;

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

   forn(i, m) {

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

      in[b]++;

      G[a].push_back(b);

   }

   for1(i, n) {

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

   }

   while(!q.empty()) {

      int now = q.front();

      q.pop();

      int sz = G[now].size();

      forn(i, sz) {

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

         in[G[now][i]]--;

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

      }

   }

   for1(i, n) {

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

   }

   cout << ans << endl;

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

using namespace std;

#define ll long long

#define ull unsigned long long

#define mst(a, b) memset((a), (b), sizeof(a))

#define mp(a, b) make_pair(a, b)

#define pi acos(-1)

#define pii pair<int, int>

#define pb push_back

const int INF = 0x3f3f3f3f;

const double eps = 1e-10;

const int maxn = 1e5 + 7;

const int maxm = 5e5 + 10;

const int mod = 1e9 + 7;

vector<int> s[maxn];

int vis[maxn];

int dp[maxn];

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    mst(vis, 0);

    mst(dp, 0);

    queue<int> q;

    int n, m;

    cin >> n >> m;

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

    {

        int x, y;

        cin >> x >> y;

        vis[y]++;

        s[x].pb(y);

    }

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

    {

        if (!vis[i])

            q.push(i);

    }

    while (!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

            vis[s[now][i]]--;

            if (vis[s[now][i]] == 0)

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

        }

    }

    int ans = 0;

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

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

    cout << ans << endl;

    return 0;

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

using namespace std;

const int N=100005;

int dp[N],vis[N];

vector<int>e[N],p;

void dfs(int v)

{

	vis[v]=1;

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

		if(!vis[e[v][i]])

			dfs(e[v][i]);

	p.push_back(v);

}

int main()

{

	int n,m;

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

	}

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

		if(!vis[i])

			dfs(i);

	reverse(p.begin(),p.end());

	int ans=0;

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

	{

		int v=p[i];

		for(int j=0;j<e[v].size();j++)

			dp[e[v][j]]=max(dp[e[v][j]],dp[p[i]]+1);

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

	}

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

	return 0;

}


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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

using namespace std;



typedef long long ll;

typedef long double ld;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;



const int mx=1e5+5;



int dp[mx];

vector<int>g[mx];



int path(int u)

{

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

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

	for(auto it: g[u])

	{

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

	}

	return dp[u];

}		



void solve()

{

	int n,m;

	cin>>n>>m;

	

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

	{

		int u,v;

		cin>>u>>v;

		g[u].push_back(v);

	}

	

	int ans=0;

	

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

	{

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

	}

	cout<<ans;

}



int main()

{

	ios::sync_with_stdio(false);

	cin.tie(0); cout.tie(0);

	

	solve();

							                                				

	cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";

	

	return 0;

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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



using namespace std;



#define el				'\n'

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

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

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



double const EPS = 1e-8, PI = acos(-1);

const int N = 1e5 + 9, M = 50 + 9, OO = (int) 1e9 + 1;

const long long MOD = 1e9 + 7;



typedef long long ll;



void OUTPUT() {

	cout << fixed << setprecision(12);

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

}



void INPUT() {

#ifndef ONLINE_JUDGE

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

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

#endif

	cout << fixed << setprecision(0);

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

}



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

vector<vector<int>> edges(N);



int DFS(int node) {

	if (vis[node] != -1)

		return vis[node];

	vis[node] = 0;

	for (auto child : edges[node]) {

		DFS(child);

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

	}

	return 0;

}



int main() {

//	INPUT();

	OUTPUT();

	clr(vis, -1);

	cin >> n >> m;

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

		cin >> x >> y;

		edges[x].push_back(y);

	}

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

		DFS(j);

	}

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

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

	}

	cout << ans;



	return 0;



}


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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

}
// Slower C++ solution:
// Author : Syed Mujtaba

// Seek knowledge from the Cradle to the Grave !!

// https://www.linkedin.com/in/syed-mujtaba-064a5016a 



#include <bits/stdc++.h>

using namespace std;

#define int     long long

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

#define mod ((int)(1e9 + 7))

vector < int > to[100005];

vector < int > from[100005];

vector < int > roots;

int dp[100005];

int cal(int i)

{

	if(dp[i] != -1)

		return dp[i];

	int dist = -1;

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

	{

		dist = max(dist, 1 + cal(to[i][j]));

	}

	return dp[i] = dist;

}

signed main()

{

	fast_io

	int n, m;

	int x, y;

	cin >> n >> m;

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

	{	

		cin >> x >> y;

		to[y].push_back(x);

		from[x].push_back(y);

	}

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

		dp[i] = -1;

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

	{

		if(to[i].size() == 0)

		{

			roots.push_back(i);

			dp[i] = 0;

		}

	}

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

	{

		if(dp[i] == -1)

			dp[i] = cal(i);

	}

	int ans = -1;

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

	{

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

	}

	cout << ans << endl;

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

#define fastIO      ios::sync_with_stdio(0);cin.tie(0);

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

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

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

}