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





using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef double dd;

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

#define endl "\n"

#define clr(n, r) memset(n,r,sizeof(n));



void fast() {

    cin.tie(0);

    cin.sync_with_stdio(0);

}



int OO = 100000000,n,m;

int dp[100009];

vector<vector<int>>edges(1e5);

int ans(int i){

    int &ret=dp[i];

    if(~ret)return ret;

    if(i==n+1||edges[i].empty())return 0;

    ret =0;

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

        ret=max (ret,1+ans(edges[i][j]));

    }

    return ret;

}







int main() {

    fast();

    cin>>n>>m;

    clr(dp,-1);

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

        int a,b;cin>>a>>b;

        edges[a].push_back(b);

    }

    int s=0;

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

     s=max(s,ans(j));

    }

    cout<<s;

}

// Faster C++ solution for the same problem (measured speedup: 1.1291394414014073x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

using namespace std;

#define ll long long

#define ull unsigned long long

#define PII pair<int,int>

#define PSI pair<string,int>

#define PIII pair< int, pair<int,int> >

const int MaxN = 1e5+5;

vector<int> g[MaxN];

int dp[MaxN];

int N;



int dfs(int node){



    auto &ret = dp[node];

    if(ret!=-1) return ret;

    ret = 0;

    for(auto i: g[node])

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

    return ret;

}



void read(){



    int i,edge;

    memset(dp,-1,sizeof dp);

    cin>>N>>edge;

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

        int u,v;

        cin>>u>>v;

        g[u].push_back(v);

    }

}



void solve(){



    read();

    int res = 0;

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

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

    }



    cout<< res << "\n";

}



int main(){



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

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    solve();

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1234318331863764x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

using namespace std;



int n, m;

vector<int>ady[100001];

vector<int>topoSort;

vector<int>who[100001];

bool visitado[100001];

int memo[100001];



void dfs(int nodo){

    visitado[nodo] = true;

    for(int i : ady[nodo])

        if (!visitado[i])

            dfs(i);

    topoSort.push_back(nodo);

}



void topological_sort(){

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

        if(!visitado[i])

            dfs(i);

}



int dp(int nodo){

    if(who[nodo].size() == 0)//es nodo raiz

        return  1;

    if(memo[nodo])

        return memo[nodo];

    int Max = 0;

    for(auto i : who[nodo])

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

    return memo[nodo];

}



int maxPath(){

    topological_sort();

    //hacemos orden topologico

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

    //     cout<<topoSort[i]<<" ";

    // cout<<endl;

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

    //     cout<<i<<": ";

    //     for(auto j : who[i])

    //         cout<<j<<" ";

    //     cout<<endl;

    // }

    // cout<<endl;

    // exit(0);

    int Max = -1;

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

        Max = max(Max, dp(topoSort[i]));

    return Max;

}



int main(){



    cin.tie(0);

    ios_base::sync_with_stdio(0);



    cin>>n>>m;



    while(m--){

        int a, b;

        cin>>a>>b;

        ady[a].push_back(b);

        who[b].push_back(a);

    }

    cout<<maxPath() - 1;

}

// Faster C++ solution for the same problem (measured speedup: 1.8232390661903977x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

 

using namespace std;

 

typedef long long ll;

typedef vector<ll> vi;

typedef long double td;

typedef vector<td> vd;

typedef pair<ll,ll> ii;

typedef vector<ii> vii; typedef vector<bool> vb;

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

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

#define forn(i, n) forr(i, 0, n)

#define pb push_back

#define mp make_pair

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

#define DBG(x) cerr << #x << " = " << (x) << endl

#define show(v,n) cerr << #v << " = "; forn(i,n) cerr << v[i] << " "; cerr << endl;

#define esta(x,c) ((c).find(x) != (c).end())

#define RAYA cerr << "===============================" << endl

int MAXN = 500005;



vector <int> v[100005];

vector <int> dp(100005,0);

vector <bool> visto(100005,false);



void DFS(int n)

{

	visto[n]=true;

	for(auto u : v[n])

	{

		if(!visto[u]) DFS(u);

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

	}

}

 

int main()

{ 	

	FIN;

	#ifdef input

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

	#endif



	int n, m;

	cin >> n >> m;

	forn(i,m)

	{

		int x, y;

		cin >> x >> y;

		v[x].pb(y);

	}

	forn(i,n+1)

	{

		if(visto[i]==false) DFS(i);

	}

	int ans = 0;

	forn(i,n+1) ans = max(ans,dp[i]);

	cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1280243359328228x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

#define endl '\n'

using namespace std;



const int MXN = 1e5 + 5;

int n, m;

int dp[MXN];

vector<int> g[MXN];



int dfs(int u) {

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

    int ans = 0;

    for(auto & v: g[u]) {

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

    }

    return dp[u] = ans;

}



int main() {

    ios_base::sync_with_stdio(false); cin.tie(NULL);

    cin >> n >> m;

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

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

        u--, v--;

        g[u].emplace_back(v);

    }

    memset(dp, -1, sizeof dp);

    int ans = 0;

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

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

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.12263366669545x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

#include<algorithm>

#include<queue>

#include<cstring>

#include<cstdio>

#include<cmath>

#include<set>

#include<map>

#include<vector>

#include<cmath>

#include<ctime>



using namespace std;

const int maxn = 1e5 + 7;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

const double pi = acos(-1.0);

typedef long long ll;

#define pb push_back



int n, m;

int dp[maxn];

int in[maxn];



int main(){

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

	queue<int>q;

	vector<int> vt[maxn];

	memset(dp, 0, sizeof dp);

	int u, v;

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

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

		vt[u].pb(v);

		in[v]++;

	}

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

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



	int now;

	int ans = 0;

	while(!q.empty()){

		now = q.front();

		q.pop();

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

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

			int t = vt[now][i];

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

			in[t]--;

			if(in[t] == 0) q.push(t);

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

		}

	}

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

}

// Faster C++ solution for the same problem (measured speedup: 1.1206152306141683x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

#define ll long long

#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

double PI=acos(-1);

vector<int> adj[100005];

int dp[100005];

bool vis[100005];

int solve(int node){

    if(vis[node])

        return dp[node];

    vis[node]=1;

    int ret=1;

    for(auto v:adj[node])

        ret=max(ret,solve(v)+1);

    return dp[node]=ret;

}



int main(){

    int n,m,ans=0;cin>>n>>m;

    for(int i=0;i<100005;i++) dp[i]=1;

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

        int x,y;cin>>x>>y;

        adj[x].push_back(y);

    }

    for(int i=1;i<=n;i++) ans=max(ans,solve(i));

    cout<<ans-1<<'\n';















    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1158563679537887x):
#include <cstdio>

#include <cstring>

#include <iostream>

#include <queue>

#include <cmath>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#include <vector>

#include <stdlib.h>

#include <algorithm>

using namespace std;





typedef long long ll;

const int MAX_N = 100025;

int dp[MAX_N],in[MAX_N];

vector<int > G[MAX_N];

int main()

{

   

    queue<int > q;

    int n,m,a,b,ans = 0;scanf("%d%d",&n,&m);

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

	scanf("%d%d",&a,&b),in[b]++,G[a].push_back(b);

    for(int i = 1;i<=n;++i) if(!in[i]) q.push(i);

    while(!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

        }

    }

    for(int i = 1;i<=n;++i) ans = max(ans,dp[i]);

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

    return 0;

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

#include<bits/stdc++.h>



using namespace std;



int n,m;

vector<int>v[200005];

int ans=0,c=0;

int dp[200005];

int vis[200005];

int mx=0;

void dfs(int node){

    vis[node]=1;

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

            if(!vis[v[node][j]])

            dfs(v[node][j]);

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

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

    }

    }





int main()

{

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

    }

   }

   cout<<mx;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.6157644796557584x):
#include<iostream>

#include<iomanip>

#include<cstdio>

#include<cmath>

#include<vector>

#include<string>

#include<algorithm>

#include<queue>

#include<set>

#include<map>

#include<limits>

#include<stack>

#include<array>

#include<stdio.h>

#include<cstring>



using namespace std;



typedef long long ll;

typedef pair<int,int> ii;

typedef vector<pair<int,int> > vii;

typedef vector<vii> vvii;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vll;

typedef vector<vll> vvll;

typedef vector<bool> vb;

typedef vector<vb> vvb;

typedef vector<char> vc;

typedef vector<vc> vvc;

typedef vector<string> vs;



int INF = std::numeric_limits<int>::max();

ll LLINF = 1e15;

int NINF = std::numeric_limits<int>::min();

int MOD = 1e9+7;



int col[] = {0,-1,0,1,1,1,-1,-1};

int row[] = {-1,0,1,0,1,-1,1,-1};



//knight moves

int cc[] = {1,2,2,1,-1,-1,-2,-2};

int rr[] = {-2,-1,1,2,-2,2,1,-1};



vvi adjLst(1e5+5,vi());

vb vstd(1e5+5,0);

vi dp(1e5+5,1);





void dfs(int u)

{

    vstd[u] = 1;

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

    {

        if(!vstd[adjLst[u][i]])

            dfs(adjLst[u][i]);



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

    }

}





int main()

{

    //cout << LLINF ;

    ios::sync_with_stdio(0);

    cin.tie(0);



    int n, m, u, v;

    cin >> n >> m;

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

    {

        cin >> u >> v;

        adjLst[u].push_back(v);

    }



    int mx = 0;

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

    {

        if(!vstd[i])

            dfs(i);

    }



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

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



    cout << mx-1;



    return 0;

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

}


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

#define f first

#define s second

#define pb push_back

#define ENDL '\n'

#define all(a) begin(a), end(a)

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

#define deb(a) cout << #a << ": " << a << ENDL

#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)

#define _ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



using namespace std;



typedef long long lli;

typedef long double ld;

typedef pair<lli, lli> ii;

typedef vector<lli> vi;



vector<vector<lli>> graph;

vector<lli> dp;



int dfs(int u) {

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

    int ret = 0;

    for(auto it : graph[u]) {

        ret = max(ret, dfs(it) + 1);

    }

    dp[u] = ret;

    return ret;

}



int main()

{   _

    lli n, m, u, v; cin >> n >> m;

    graph.resize(n + 1);

    dp.resize(n + 1);

    fill(all(dp), -1);

    fore(i, 0, m) {

        cin >> u >> v;

        graph[u].pb(v);

    }

    int mx = 0;

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

    cout << mx << ENDL;

    return 0;

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

using namespace std;

typedef long long int ll;

const int MAX=1e5 + 10;

int n,m,Sub[MAX];

vector<int>g[MAX];

bool vis[MAX];

int dfs(int root)

{

    if(vis[root])return Sub[root];

    vis[root]=1;

    int ans=0;

    for(int x:g[root]){

            ans=max(ans,1+ dfs(x));

    }

    return Sub[root]=ans;

}

int main ()

{

    cin >> n >> m;

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

        int u,v;

        cin >> u >> v;

        g[u].push_back(v);

    }

    int ans=0;

    for(int i=1;i<=n;i++) if(!vis[i])ans = max(ans,dfs(i));

    cout<<ans;

    return 0;

}


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

#define f first

#define s second

#define pb push_back

#define ENDL '\n'

#define all(a) begin(a), end(a)

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

#define deb(a) cout << #a << ": " << a << ENDL

#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)

#define _ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



using namespace std;



typedef long long lli;

typedef long double ld;

typedef pair<lli, lli> ii;

typedef vector<lli> vi;



vector<vector<lli>> graph;

vector<lli> dp;



int dfs(int u) {

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

    int ret = 0;

    for(auto it : graph[u]) {

        ret = max(ret, dfs(it) + 1);

    }

    dp[u] = ret;

    return ret;

}



int main()

{   _

    lli n, m, u, v; cin >> n >> m;

    graph.resize(n + 1);

    dp.resize(n + 1);

    fill(all(dp), -1);

    fore(i, 0, m) {

        cin >> u >> v;

        graph[u].pb(v);

    }

    int mx = 0;

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

    cout << mx << ENDL;

    return 0;

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

using namespace std;



#define MX 100005

#define ll long long



vector < int > G[MX];

vector < int > topsort;

bool vis[MX];

ll dp[MX];



void dfs(int n){

    vis[n] = 1;

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

        int nxt = G[n][i];

        if (!vis[nxt])

            dfs(nxt);

    }

    topsort.push_back(n);

}



void solve(){

    for (int x = 0; x < topsort.size(); x++){

        int cur = topsort[x];

        if (G[cur].size() == 0){

            dp[cur] = 0;

        }

        else{

            ll mx = 0;

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

                int nxt = G[cur][i];

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

            }

            dp[cur] = mx + 1;

        }

    }





}



int main()

{

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

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

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

        G[u].push_back(v);

    }



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

        if (!vis[i])

            dfs(i);

    }

    solve();

    ll res = 0;

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

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



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

    return 0;

}




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

#define f first

#define s second

#define pb push_back

#define ENDL '\n'

#define all(a) begin(a), end(a)

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

#define deb(a) cout << #a << ": " << a << ENDL

#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)

#define _ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



using namespace std;



typedef long long lli;

typedef long double ld;

typedef pair<lli, lli> ii;

typedef vector<lli> vi;



vector<vector<lli>> graph;

vector<lli> dp;



int dfs(int u) {

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

    int ret = 0;

    for(auto it : graph[u]) {

        ret = max(ret, dfs(it) + 1);

    }

    dp[u] = ret;

    return ret;

}



int main()

{   _

    lli n, m, u, v; cin >> n >> m;

    graph.resize(n + 1);

    dp.resize(n + 1);

    fill(all(dp), -1);

    fore(i, 0, m) {

        cin >> u >> v;

        graph[u].pb(v);

    }

    int mx = 0;

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

    cout << mx << ENDL;

    return 0;

}