stablegradients/pie-gem5-pairs huggingface dataset:
// 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.1059732199620915x):
#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;

}
// 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.2300689200522588x):
#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;

}
// 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.2300689200522588x):
#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;

}
// 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.104266655781063x):
#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;

}
// 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.6530874726185574x):
#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;

}
// 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.0770142408924284x):
#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;

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



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

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

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

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



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

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

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

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



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

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

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

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



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

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

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

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



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

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

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

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

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

using namespace std;

typedef long long int ll;



vector<ll>adj[100005];

bool visited[100005];

ll dp[100005];



void dfs(ll s)

{

    ll len=1;

    visited[s]=true;

    for(auto x : adj[s])

    {

        if(visited[x]==false)

            dfs(x);



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

    }

}





int main()

{

    ll n,m,x,y;

    cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].push_back(y);

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

    }



    ll mx_len=0;



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

    {

        if(visited[i]==false)

            dfs(i);

        

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

    }



    cout<<mx_len<<endl;



    return 0;

}