stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 2.649473364546905x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxN = 1e5 + 10;



int n, m;

vector<int> g[maxN];

int vis[maxN];

int dp[maxN];

stack<int> s;

vector<int> ver;



void dfs(int u) {

  s.push(u);

  vis[u] = 1;

  for (auto v : g[u]) {

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

  }

  ver.push_back(u);

  s.pop();

}



int main(){

//  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);

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

  cin >> n >> m;

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

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

    g[u].push_back(v);

  }

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

    if (!vis[i]) {

      dfs(i);

    }

  }

  int ans = 0;

  for (int i = 0; i < ver.size(); ++i) {

    int u = ver[i];

    for (auto v : g[u]) {

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

    }

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

  }

  cout << ans;

return 0;

}
// 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: 2.0741848754793324x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxN = 1e5 + 10;



int n, m;

vector<int> g[maxN];

int vis[maxN];

int dp[maxN];

stack<int> s;

vector<int> ver;



void dfs(int u) {

  s.push(u);

  vis[u] = 1;

  for (auto v : g[u]) {

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

  }

  ver.push_back(u);

  s.pop();

}



int main(){

//  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);

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

  cin >> n >> m;

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

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

    g[u].push_back(v);

  }

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

    if (!vis[i]) {

      dfs(i);

    }

  }

  int ans = 0;

  for (int i = 0; i < ver.size(); ++i) {

    int u = ver[i];

    for (auto v : g[u]) {

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

    }

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

  }

  cout << ans;

return 0;

}
// 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: 1.1194355504217501x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxN = 1e5 + 10;



int n, m;

vector<int> g[maxN];

int vis[maxN];

int dp[maxN];

stack<int> s;

vector<int> ver;



void dfs(int u) {

  s.push(u);

  vis[u] = 1;

  for (auto v : g[u]) {

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

  }

  ver.push_back(u);

  s.pop();

}



int main(){

//  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);

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

  cin >> n >> m;

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

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

    g[u].push_back(v);

  }

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

    if (!vis[i]) {

      dfs(i);

    }

  }

  int ans = 0;

  for (int i = 0; i < ver.size(); ++i) {

    int u = ver[i];

    for (auto v : g[u]) {

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

    }

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

  }

  cout << ans;

return 0;

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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

#define max3(a,b,c) max(a,max(b,c))

const ll INF = 1 << 30;

typedef pair<double, double  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<double,payar  > > vpp;

vector < payar >  vp;

vector<int>g[100005];

int ind[100005];

int vis[100005];

int dis[100005];



int dfs(int u)

{



    vis[u]=1;

    if(dis[u]!=-1)return dis[u];

    int maxi =0;

    for(auto it: g[u])

    {

       // static int k;

     //   cout<<k++<<" "<<it<<endl;



      dis[u]=  maxi=max(maxi,1+dfs(it) );

    }

    return maxi;

}

int main()

{



    int n, m ;

    cin>>n>>m;

    memset(dis,-1,sizeof dis);

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

    {

        int u, v;

        cin>>u>>v;

        g[u].push_back(v);

        ind[v]++;



    }

    int ans =0;

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

    {

        if(ind[i]==0 and vis[i]==0)

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

    }

    cout<<ans;

    return  0 ;

}


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

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

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



        }

    }

    cout << ans << endl;

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

}

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

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

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



        }

    }

    cout << ans << endl;

}
// 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: 1.2414965359196095x):
# include <bits/stdc++.h>

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

            ans = max( ans, dfs( 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: 1.1145255404270176x):
# include <bits/stdc++.h>

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

            ans = max( ans, dfs( 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: 2.6777352496701696x):
# include <bits/stdc++.h>

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

            ans = max( ans, dfs( 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: 2.0963101685506342x):
# include <bits/stdc++.h>

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

            ans = max( ans, dfs( 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: 1.1313765494716983x):
# include <bits/stdc++.h>

# define int long long

# define fi first

# define se second



using namespace std ;



const int N = 1e5 + 1 ;

int v[N], us[N], mx, ans ;

vector < int > vc[N] ;



int dfs( int u ) {

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

    int mx = 0 ;

    for( auto i : vc[u] ) {

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

    }

    us[u] = mx ;

    return mx ;

}



int32_t main() {

    //freopen("haybales.in", "r", stdin ) ;

    //freopen("haybales.out", "w", stdout ) ;

    int n, m ;

    cin >> n >> m ;

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

        int x, y ;

        cin >> x >> y ;

        vc[x].push_back(y) ;

    }

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

        if( !us[i] ) {

            ans = max( ans, dfs( 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: 1.2795799806127404x):
#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);

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

}

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

}