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

#define ll long long

using namespace std;

ll n,m,x,y;

vector<ll>v[100009];

ll dp[100009];



ll f(ll N){

	if (v[N].empty())return 0;

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

	ll z=0;

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

		z=max(z,f(v[N][i])+1);

	}

	dp[N]=z;

	return dp[N];

}



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

	}

	ll jwb=0;

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

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

	}

	cout << jwb << endl;

}//z

// Faster C++ solution for the same problem (measured speedup: 2.399714436826119x):
#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;

	}

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





using namespace std;



vector<int> v[100005];

bool processado[100005];

int tab[100005];



int dfs(int s)

{



    //processado[s] = true;

    int soma, max = 0, entrou = 0;

    

    if(tab[s] >= 0)

    {

        return tab[s];

    }

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

    {



        entrou = 1;

        soma = dfs(v[s][i]) + 1;

        if(soma > max)

        {

            max = soma;

            tab[s] = max;

        }

    }

    if(entrou == 1)

    {

        return max;

    }



    return tab[s] = 0;



}

 





int main(){



    int n, m;

    cin >> n >> m;

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



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

    {

        processado[i] = false;

    }*/



    int x, y;

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

    {

        cin >> x >> y;

        v[x].push_back(y);

    }



    int soma = 0;

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

    {

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

    }



    cout << soma << endl;











    



    

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.154289562792087x):
#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;

	}

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



using namespace std;



vector < int > adj[100007];



int used[100007], dp[100007], mx;



void dfs( int v ) {

	if ( used[v] ) return;

	used[v] = 1;

	for ( auto u: adj[v] ) {

		dfs(u);

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

	}

}



int main() {

	int n, m;

	cin >> n >> m;

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

		int u, v;

		cin >> u >> v;

		adj[u].push_back(v);

	}

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

		dfs(i);

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

	}

	cout << mx << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.9378086557601288x):
#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;

	}

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

using namespace std;

const int N = 1e5 + 100;

int n, m, mem[N];

bool vis[N];

vector<int> adjList[N];



int dp(int v) {

	if (vis[v]) {

		return mem[v];

	}

	vis[v] = true;



	int ans = 0;

	for(int u : adjList[v]) {

		ans = max(ans, dp(u) + 1);

	}



	return mem[v] = ans;

}



int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);



	cin >> n >> m;



	int a, b;

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

		cin >> a >> b;

		adjList[a].emplace_back(b);

	}



	int ans = 0;

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

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

	}



	cout << ans << '\n';



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.9491919349044966x):
#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;

	}

}
// Slower C++ solution:
/**

     **author : _KMS_

     **created : 30-03-2020

**/

#include<bits/stdc++.h>

using namespace std;

const long long M =3e5+10,MOD=1e9+7;

typedef long long ll;

vector<int>v[M];

bool vis[M];

int dp[M];

void dfs(int i)

{

  vis[i]=1;

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

  {

    int y=v[i][j];

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

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

  }

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

     int n,m;

     cin>>n>>m;

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

     {

       int x,y;

       cin>>x>>y;

       v[x].push_back(y);

     }

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

     {

      if(!vis[i])

      {

        dfs(i);

      }

     }

    sort(dp+1,dp+n+1);

    cout<<dp[n]<<"\n";

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.175850073453437x):
#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;

	}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.9484384805985298x):
#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;

	}

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

#define ll long long



using namespace std;

int dp[200005],n,m;

vector <int> DAG[200005];

int dfs(int x)

{

    if(dp[x]!=-1)

        return dp[x];

    int retVal=0;

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

    {   int child=DAG[x][i];

        retVal=max(retVal,dfs(child)+1);

    }

    dp[x]=retVal;

    return dp[x];

}

int main()

{

    cin>>n>>m;

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

    {

        int x,y;

        cin>>x>>y;

        DAG[x].push_back(y);

    }

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

    int longestPath=0;

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

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



    cout << longestPath << endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.052001459520862x):
#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;

	}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.9371539294686362x):
#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;

	}

}
// Slower C++ solution:
#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;

}






// Faster C++ solution for the same problem (measured speedup: 1.9380738895470184x):
#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;

	}

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 2.1868612281247293x):
#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;

	}

}
// Slower C++ solution:
#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; 

}

// Faster C++ solution for the same problem (measured speedup: 1.9297516926177287x):
#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;

	}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.9439754921010466x):
#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;

	}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.9508780343171086x):
#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;

	}

}