stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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);

}


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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

}
// Slower C++ solution:
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long

#define ull unsigned long long

#define db long double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

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

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

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



//#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair <int, int> pii;



const int N = 1e5 + 7;

int n, m, dp[N];

vector <int> q[N], r[N], ord;

bool used[N];



void dfs(int v) {

    used[v] = true;

    for (auto i : q[v]) {

        if (!used[i]) {

            dfs(i);

        }

    }

    ord.pb(v);

}



void solve(int tc) {

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

    cin >> n >> m;

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

        int x, y;

        cin >> x >> y;

        q[x].pb(y);

        r[y].pb(x);

    }

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

        if (!used[i]) {

            dfs(i);

        }

    }

    reverse(all(ord));

    int res = 0;

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

        int v = ord[i - 1];

        for (auto j : r[v]) {

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

        }

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

    }

    cout << res;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

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

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

    int tc = 1;

//    cin >> tc;

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

        solve(i);

//        cleanup();

    }

}


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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

using namespace std;

typedef long long ll;

#define fast ios_base::sync_with_stdio(false),cin.tie(NULL)

#define INF 1e18

int n, m;

vector<int> graph[100020];

vector<int> dp(100020, -1);

vector<bool> visited(100020, false);

int dfs(int node, int sum)

{

    if(dp[node] != -1)

        return dp[node];

    if(graph[node].empty())

        return 1;

    else{

        int temp, max = 0;

        for (auto child : graph[node]) {

            temp = dfs(child, sum+1);

            if(temp > max)

                max = temp;

        }

        return dp[node] = max+1;

    }

}

int check()

{

    int mx = 0;

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

        if(!visited[i])

            mx = max(mx, dfs(i, 0));

    }

    return mx-1;

}

int main() {

    fast;

    cin>>n>>m;

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

        int a, b;

        cin>>a>>b;

        graph[a].push_back(b);

    }

    cout<<check();

}

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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

using namespace std;

#define mx 200005

vector<int>adj[mx];

bool vis[mx];

int dp[mx];



void dfs(int src){

vis[src] = 1;



for(int i = 0;i<adj[src].size(); i++){

    if(!vis[adj[src][i]])

        dfs(adj[src][i]);

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

}

}

int longest_path(int n){

    int ans = 0;

    memset(dp,0,sizeof dp);

    memset(vis,0,sizeof vis);



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

        if(!vis[i])

            dfs(i);

    }

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

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

    }

return ans;

}









int main(){

int n,m;

cin>>n>>m;

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

    int x,y;

    cin>>x>>y;

    adj[x].push_back(y);

}



cout<<longest_path(n)<<endl;

}


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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

#include <stdio.h>

#include <string>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>

#include <set> 

#include <map>

#include <queue>

#include <deque>

#include <stack>

#include <bitset>

#include <iomanip>

using namespace std;

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

#define ll long long

#define ull unsigned long long

#define ld long double

#define ii pair<int, int>

#define pll pair<ll, ll>

#define vii vector<ii>

#define f(i,a,b) for(int i=a; i<b; i++)

#define F first

#define S second

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

#define ReadFile freopen("input.txt","r",stdin)

#define WriteFile freopen("output.txt","w",stdout)

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

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

const int MOD = 998244353, N = 2e5 + 5;

const double pi = acos(-1);



vector<int> graph[N];

bool vis[N];

int dp[N];



void dfs(int node)

{

	if (vis[node]) return;

	vis[node] = true;

	for (int child : graph[node])

	{

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

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

	}

}



int main()

{

	assem99;



	int n, m; cin >> n >> m;

	f(i, 0, m)

	{

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

		graph[u].push_back(v);

	}

	int ans = 0;

	f(i, 1, n + 1) if (!vis[i]) dfs(i);

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



	cout << ans;



	return 0;

}


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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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



/**

       return NO_PAIN ? NO_GAIN : GAIN;

*/



using namespace std;



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

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

#define Unique(x) x.erase(unique(all(x)), x.end())

#define endl "\n"

#define pb push_back

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

#define isOdd(n)    (n&1)

#define pow(n, m) (ll)powl(n, m)

#define numOfDigits(n)  ((n)? (ll)(log10(n))+1 : 1)

#define clr(x, val)    memset(x, val, sizeof(x)) //be careful of value and TLE

#define MOD 1000000007

#define EPS 1e-7

#define LSOne(x)  (x & (-x))   //least significant 1

typedef long long ll;

typedef long double ld;

typedef pair<int, int> pii;

typedef vector<int> vi;



const int N = 1e5 + 1;

vector<vi> adj(N);

bool vis[N];

int dp[N];

void dfs(int u){

    /*

    for each vertex, ask it what is the longest path for you.

     then his parent's longest path is max + 1

    */

    vis[u] = 1;

    for(int i = 0; i<sz(adj[u]); i++){

        int v = adj[u][i];

        if(!vis[v])

            dfs(v);

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

    }

}

int main()

{

    START

    //freopen("test cases\\in.txt", "r", stdin) ;

    //freopen("test cases\\in.txt", "w", stdout);

    int n, m; cin>>n>>m;



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

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

        adj[u].pb(v);

    }

    int mx = 0;

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

        if(!vis[i] && sz(adj[i]))

            dfs(i);



    cout<<*max_element(dp, dp+N);

	return 0;

}


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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

using namespace std ;

#define ll long long



inline int D(){

    int t ;

    scanf( "%d" , &t ) ;

    return t ;

}

inline ll LLD(){

    ll t ;

    scanf( "%lld" , &t ) ;

    return t ;

}

//const int INF = 0x3f3f3f3f;

//const int mod=1e9+7;

//###################################################################

const int N=100009;

int n,m;

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



int mem[N];

int solve(int num){

    if(mem[num]!=-1)return mem[num];

    if(g[num].size()) {

        int ret=0;

        for (auto i:g[num]) {

            ret =max(ret,solve(i));

        }

        return mem[num]=1+ret;

    }

    return mem[num]=0;



}



int main(){

    n=D(),m=D();

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

        int u=D(),v=D();

        g[u].push_back(v);

    }

    int mx=0;

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

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

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

    }

    printf("%d",mx);

}

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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

using namespace std;



vector <int> dp[100010], ed[100010];



int solve(int i, int j){

	

	if(j == (int)ed[i].size()) return 0;

	

	if(dp[i][j] == -1) dp[i][j] = max(solve(ed[i][j], 0) + 1, solve(i, j + 1));

	

	return dp[i][j];

}

	



int main(){

	

	int n, m, ans = 0;

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

	

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

		int a, b;

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

		

		ed[a].push_back(b);

		dp[a].push_back(-1);

	}

	

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

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

			if(dp[i][j] == -1) solve(i, j);

			

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

		}

	}

	

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

	

	return 0;

}


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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

using namespace std;





#define clr(arr,val) memset(arr,val ,sizeof arr)

typedef  long long ull;

typedef long long ll;





ll const oo=1e9 ;

int const SZ= (int)1e5+3;

const ll mod=(ll)1e9+7;





vector<int> adj[SZ];

//int arr[100];

int dp[SZ];

int n,m;



int solve(int i){



    int &ret=dp[i];

    if(~ret) return ret;

    int mx=0;

    for(int x: adj[i]){

        mx=max(mx,solve(x));

    }

    return ret=1+mx;

    

}

int main()

{

    

    cin>>n>>m;

    int u,v;

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

        cin>>u>>v; 

        adj[u].push_back(v);

    }

    clr(dp,-1);

    int mx=0;

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

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

    }

    

    cout<<mx-1<<endl;

    

    return 0;

}

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

using namespace std;

const int maxn = 1e5+5;

vector<int> adj[maxn];

int dis[maxn];



int dfs(int cur)

{

	if(dis[cur] != 1) return dis[cur];

	else{



		int temp = 0;

		for(auto i : adj[cur]){

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

		}

		dis[cur] += temp;

	}

	return dis[cur];

}

int main(int argc, char const *argv[])

{

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

	int x, y;

	fill(dis, dis + n + 1, 1);

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

		scanf("%d %d", &x, &y);

		adj[x].push_back(y);

	}

	int ans = 1;

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

	/*	

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

		printf("%d ", dis[i]);

	}

	*/

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



	return 0;

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

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

#define ll long long

#define pb push_back

using namespace std;

vector<ll>adj[100005];

vector<ll>indegree(100005,0),dp(100005,-1);

ll dfs(ll node)

{

    if(adj[node].size()==0)

    return dp[node]=1;

    if(dp[node]!=-1)

    return dp[node];

    ll x=0;

    for(auto u:adj[node])

    {

        if(dp[u]==-1)

        x=max(x,dfs(u));

        else

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

    }

    return dp[node]=x+1;

}

int main() {

    ll n,m,i,x,y;cin>>n>>m;

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

    {

        cin>>x>>y;

        adj[x].pb(y);

        indegree[y]++;

    }

    ll  ans=0;

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

    {

        if(indegree[i]==0)

        {

            dfs(i);

        }

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

    }

    cout<<ans-1;

    return 0;

}


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

#define ll long long int

#define db long double

#define mp make_pair

#define F first

#define S second

#define pb push_back

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

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

#define Nmax 1000005

#define INF 1000000000

#define MOD 1000000007

#define MAXN 1000005

 

using namespace std;



vector<ll> g[100001];

ll dp[100001],vis[100001];



void dfs(ll node,ll par)

{

	vis[node]=1;

	for(auto x:g[node])

	{

		if(x!=par && !vis[x])

			dfs(x,node);

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

	}

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    int tests=1,t=1;

    // cin>>tests;

    while(tests--)

    {	

    	ll i,j,n,m;

    	cin>>n>>m;

    	rep(i,1,m)

    	{

    		ll u,v;

    		cin>>u>>v;

    		g[u].pb(v);

    	}



    	rep(i,1,n)

    	{

    		if(!vis[i])

    			dfs(i,0);

    	}

    	ll ans=0;

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

    	cout<<ans;





    }

    	return 0;

    

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

#include<sstream>

#include<fstream>

#include<string>

#include<vector>

#include<deque>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<algorithm>

#include<functional>

#include<utility>

#include<bitset>

#include<cmath>

#include<cstdlib>

#include<ctime>

#include<cstdio>

using namespace std;

int n,m,dp[202020],in[202020];

vector<int>v[202020];

queue<int>tpx;

int main()

{

	cin>>n>>m;

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

	{

		int a,b;

		cin>>a>>b;

		a--;

		b--;

		v[a].push_back(b);

		in[b]++;

	}

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

	{

		if(!in[i])

		{

			tpx.push(i);

		}

	}

	while(tpx.size())

	{

        int i=tpx.front();

        tpx.pop();

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

		{

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

            in[v[i][j]]--;

            if(!in[v[i][j]])

			{

				tpx.push(v[i][j]);

			}

        }

    }

	int ans=0;

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

	{

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

	}

	cout<<ans<<endl;

	return 0;

}

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

#include<sstream>

#include<fstream>

#include<string>

#include<vector>

#include<deque>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<algorithm>

#include<functional>

#include<utility>

#include<bitset>

#include<cmath>

#include<cstdlib>

#include<ctime>

#include<cstdio>

using namespace std;

int n,m,id[101010],l[101010],bf[101010];

vector<int>g[101010];

int main()

{

	cin>>n>>m;

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

	{

		int x,y;

		cin>>x>>y;

		x--;

		y--;

		g[x].push_back(y);

		id[y]++;

	}

	fill(bf,bf+n,-1);

	stack<int>s;

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

	{

		if(id[i]==0)

		{

			s.push(i);

		}

	}

	while(s.size())

	{

		int p=s.top();

		s.pop();

		for(int i=0;i<g[p].size();i++)

		{

			int to=g[p][i];

			if(l[p]+1>l[to])

			{

				l[to]=l[p]+1;

			}

			id[to]--;

			if(!id[to])

			{

				s.push(to);

			}

		}

	}

	cout<<*max_element(l,l+n)<<endl;

	return 0;

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

#include <cstdio>

#include <cstring>

#include <queue>

using namespace std;



const int MaxN = 100000 + 5, MaxM = 100000 + 5;



int N, M; int cnte;

int Head[MaxN], To[MaxM], Next[MaxM];

int ind[MaxN], dp[MaxN];

queue <int> que;



inline void add_edge(int from, int to) {

  cnte++; To[cnte] = to;

  Next[cnte] = Head[from]; Head[from] = cnte;

}



void init() {

  scanf("%d %d", &N, &M);

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

    int u, v;

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

    add_edge(u, v);

    ind[v]++;

  }

}



void solve() {

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

    if (ind[i] == 0) {

      que.push(i);

      dp[i] = 0;

    }



  while (!que.empty()) {

    int u = que.front(); que.pop();

    for (int i = Head[u]; i; i = Next[i]) {

      int v = To[i];

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

      ind[v]--;

      if (ind[v] == 0) que.push(v);

    }

  }



  int ans = 0;

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

  cout << ans << endl;

}



int main() {

  init();

  solve();

  return 0;

}

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



const int MaxN = 100000 + 5, MaxM = 100000 + 5;



struct Graph {

  int cnte;

  int Head[MaxN], To[MaxM], Next[MaxM];



  inline void addEdge(int from, int to) {

    cnte++; To[cnte] = to;

    Next[cnte] = Head[from]; Head[from] = cnte;

  }

};



int N, M;

int Ind[MaxN];

int Dp[MaxN];

Graph G;



inline int max(int x, int y) { return x > y ? x : y; }



void init() {

  scanf("%d %d", &N, &M);

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

    int u, v;

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

    Ind[v]++;

    G.addEdge(u, v);

  }

}



void solve() {

  static int que[MaxN];

  int head = 1, tail = 0;

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

    if (Ind[i] == 0) que[++tail] = i;

  while (head <= tail) {

    int u = que[head++];

    for (int i = G.Head[u]; i; i = G.Next[i]) {

      int v = G.To[i];

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

      Ind[v]--;

      if (Ind[v] == 0) que[++tail] = v;

    }

  }

  int ans = 0;

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

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

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

}



int main() {

  init();

  solve();

  return 0;

}