stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
/******

author :- Mahmoud Tarek.

*******/



#include<bits/stdc++.h>



using namespace std ;



#define rg register

#define dbg(v) cerr << #v << " " << v ;

typedef long long ll ;



const int N = 1e5 + 5 ;

vector<int> adj[N] ;

int n, m, a, b ;

int dp[N] ;



int solve(int i){



   if(i != 0 && !adj[i].size()){

     return 0 ;

   }



   int & ret = dp[i] ;

   if(~ret) return ret ;

   ret = 0 ;

   if(i == 0){

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

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

   }



   for(auto j: adj[i]){

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

   }



   return ret ;

}



int main() {

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



    cin >> n >> m ;

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

        cin >> a >> b ;

        adj[a].push_back(b) ;

    }



    memset(dp, -1, sizeof dp) ;

    cout << solve(0) ;



}





/*

  Empty set of troubles.

*/




// Faster C++ solution for the same problem (measured speedup: 2.7724730663328305x):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<string>

#include<cmath>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<fstream>

#include<map>

#include<bitset>

#include<unordered_map>

#include<cstring>

#include<cctype>

using namespace std;

#define  ll long long

#define  ull unsigned long long

#define  ul unsigned long

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

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

#define pb(s) push_back(s);

#define format(n) fixed<<setprecision(n)

#define finl "\n"

#define Mohammed_Atef_Hassan fast();

#define mod 1000000007

#define INF LLONG_MAX

#define PI acos(-1)

#define clr(dp,n) memset(dp,n,sizeof dp)

#define ex(n) return cout<<n<<finl,0;

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

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

ll fix_mod(ll x, ll y)

{

	return (y + x % y) % y;

}

void fast() {

	std::ios_base::sync_with_stdio(false);

	cin.tie(nullptr); cout.tie(nullptr);

}

vector<vector<int>>edg;

int mem[(int)1e5 + 10];

int solve(int u)

{

	int& ret = mem[u];

	if (~ret)return ret;

	ret = 0;

	for (auto v : edg[u])

	{

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

	}

	return ret;

}

int main()

{

	Mohammed_Atef_Hassan

		clr(mem, -1);

		int n, m;

	cin >> n >> m;

	edg = vector<vector<int>>(n + 1);

	while (m--)

	{

		int x, y;

		cin >> x >> y;

		edg[x].push_back(y);

	}

	int mx = 0;

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

	{

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

	}

	ex(mx);

}
// Slower C++ solution:


#include<bits/stdc++.h>

using namespace std;



const int N = 1e5 + 5;

typedef long long ll;

vector<int> adj[N];

bool visited[N]={};

int nodesNum;

int dp[N];

void dfs(int node){

    if(visited[node])

        return;

visited[node]=1;

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

        int child=adj[node][i];

    if(!visited[child])

    dfs(child);

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

}

}







int main()

{

   int edges;

   cin >> nodesNum >> edges;

   while (edges--)

   {

       int x, y;

       cin >> x >> y;

       adj[x].push_back(y);

   }

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

        if(!visited[i])

      dfs(i);

   }

   int ans=0;

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

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

   }

 cout<<ans;

   return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.4888611921843062x):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<string>

#include<cmath>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<fstream>

#include<map>

#include<bitset>

#include<unordered_map>

#include<cstring>

#include<cctype>

using namespace std;

#define  ll long long

#define  ull unsigned long long

#define  ul unsigned long

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

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

#define pb(s) push_back(s);

#define format(n) fixed<<setprecision(n)

#define finl "\n"

#define Mohammed_Atef_Hassan fast();

#define mod 1000000007

#define INF LLONG_MAX

#define PI acos(-1)

#define clr(dp,n) memset(dp,n,sizeof dp)

#define ex(n) return cout<<n<<finl,0;

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

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

ll fix_mod(ll x, ll y)

{

	return (y + x % y) % y;

}

void fast() {

	std::ios_base::sync_with_stdio(false);

	cin.tie(nullptr); cout.tie(nullptr);

}

vector<vector<int>>edg;

int mem[(int)1e5 + 10];

int solve(int u)

{

	int& ret = mem[u];

	if (~ret)return ret;

	ret = 0;

	for (auto v : edg[u])

	{

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

	}

	return ret;

}

int main()

{

	Mohammed_Atef_Hassan

		clr(mem, -1);

		int n, m;

	cin >> n >> m;

	edg = vector<vector<int>>(n + 1);

	while (m--)

	{

		int x, y;

		cin >> x >> y;

		edg[x].push_back(y);

	}

	int mx = 0;

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

	{

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

	}

	ex(mx);

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

#include <set>

#include <stack>

#include <vector>

#include <map>

#include <string>

#include <string.h>

#include <cstring>

#include <queue>

#include <algorithm>

#include <math.h>



using namespace std;



typedef long long ll;





const int N = 1e5 + 5;

typedef long long ll;

vector<int> adj[N];

bool visited[N] = {};

int nodesNum;

int dp[N];

void dfs(int node) {

	if (visited[node])

		return;

	visited[node] = 1;

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

		int child = adj[node][i];

		if (!visited[child])

			dfs(child);

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

	}

}







int main()

{

	int edges;

	cin >> nodesNum >> edges;

	while (edges--)

	{

		int x, y;

		cin >> x >> y;

		adj[x].push_back(y);

	}

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

		if (!visited[i])

			dfs(i);

	}

	int ans = 0;

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

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

	}

	cout << ans;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.4888611921843062x):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<string>

#include<cmath>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<fstream>

#include<map>

#include<bitset>

#include<unordered_map>

#include<cstring>

#include<cctype>

using namespace std;

#define  ll long long

#define  ull unsigned long long

#define  ul unsigned long

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

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

#define pb(s) push_back(s);

#define format(n) fixed<<setprecision(n)

#define finl "\n"

#define Mohammed_Atef_Hassan fast();

#define mod 1000000007

#define INF LLONG_MAX

#define PI acos(-1)

#define clr(dp,n) memset(dp,n,sizeof dp)

#define ex(n) return cout<<n<<finl,0;

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

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

ll fix_mod(ll x, ll y)

{

	return (y + x % y) % y;

}

void fast() {

	std::ios_base::sync_with_stdio(false);

	cin.tie(nullptr); cout.tie(nullptr);

}

vector<vector<int>>edg;

int mem[(int)1e5 + 10];

int solve(int u)

{

	int& ret = mem[u];

	if (~ret)return ret;

	ret = 0;

	for (auto v : edg[u])

	{

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

	}

	return ret;

}

int main()

{

	Mohammed_Atef_Hassan

		clr(mem, -1);

		int n, m;

	cin >> n >> m;

	edg = vector<vector<int>>(n + 1);

	while (m--)

	{

		int x, y;

		cin >> x >> y;

		edg[x].push_back(y);

	}

	int mx = 0;

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

	{

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

	}

	ex(mx);

}
// Slower C++ solution:
/*

	Author: Luis Manuel D�az Bar�n (LUISMO)

	Problem:

	Online Judge:

	Idea:

*/

#include<bits/stdc++.h>

// Types

#define ll long long

#define ull unsigned long long

// IO

#define sf scanf

#define pf printf

#define mkp make_pair

#define fi first

#define se second

#define endl "\n"



using namespace std;



typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

const ll inf = 1e16 + 3;

const int mod = 1e9 + 7;

const int lim = 2e5 + 2;



int N, M;

vector<int> adj[lim];



int dp[lim];

bool mark[lim];



int DFS(int idx)

{

    if(mark[idx])

        return dp[idx];

    // for each adjacent

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

    {

        int nxt = adj[idx][i];

        int aux = DFS(nxt);

        dp[idx] = max(dp[idx], 1 + aux);

    }

    mark[idx] = true;

    return dp[idx];

}



void solve()

{

    cin >> N >> M;

    // reading edges

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

    {

        int xi, yi;

        cin >> xi >> yi;

        adj[xi].push_back(yi);

    }

    

    int answ = 0;



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

    {

        int aux = DFS(i);

        answ = max(answ, aux);

    }



    cout << answ;

}



void fastIO()

{

	cin.sync_with_stdio(false);

	cin.tie(0);

}



void IO()

{

	if(fopen("d:\\lmo.in","r") != NULL)

	{

		freopen("d:\\lmo.in","r",stdin);

	}

	else if(fopen("/media/Beijing/lmo.in","r") != NULL)

	{

		freopen("/media/Beijing/lmo.in", "r", stdin);

	}

}



int main()

{

	IO();



	fastIO();



	solve();

}


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



using namespace std;



#define F first

#define S second

#define modulo ll (1e9 + 7)



typedef long long ll;

typedef pair<int,int> pii;



void init(){

    cin.tie(0);

    cin.sync_with_stdio(0);

}



const int N = 1e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;

const ll llOO = 0x3f3f3f3f3f3f3f3f;





#define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])



struct Adj {

	int head[N], nxt[M], to[M], ne, n;

	void init(int n) {

		memset(head, -1, n * sizeof(head[0]));

		ne = 0;

		this->n = n;

	}

	void addEdge(int f, int t) {

		nxt[ne] = head[f];

		to[ne] = t;

		head[f] = ne++;

	}

	void addBiEdge(int f, int t) {

		addEdge(f, t);

		addEdge(t, f);

	}

	int addNode() {

		head[++n] = 0;

		return n;

	}

} adj;







int dp[N];



int dfs(int u){

    int &ret = dp[u];

    if(~ret)    return dp[u];



    ret = 0;

    neig(adj, u, e, v)

        ret = max(ret, dfs(v));



    ret++;



    return ret;

}





int n, m, u, v;



void go(int tc){

    cin >> n >> m;

    adj.init(n);

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

        cin >> u >> v;  u--, v--;

        adj.addEdge(u, v);

    }



    memset(dp, -1, sizeof dp);



    int ans = 0;

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

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



    cout << ans - 1;

}



int main(){

    init();



    int t, tc = 1;  t = 1;//cin >> t;

    while(t--)  go(tc++);



}
// 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 n,m,u,v;

vector<int> ga[100100];

int deg[100100];

int dp[100100];

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

		deg[v]++;

	}

	stack<int> s;

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

		if(deg[i]==0){

			s.push(i);

		} 

	}

	while(s.size()){

		int x=s.top();

		s.pop();

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

			int y=ga[x][i];

			deg[y]--;

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

			if(deg[y]==0){

				s.push(y);

			}

		} 

	}

	cout<<*max_element(dp+1,dp+n+1);

	return 0;

}


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



using namespace std;



#define F first

#define S second

#define modulo ll (1e9 + 7)



typedef long long ll;

typedef pair<int,int> pii;



void init(){

    cin.tie(0);

    cin.sync_with_stdio(0);

}



const int N = 1e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;

const ll llOO = 0x3f3f3f3f3f3f3f3f;





#define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])



struct Adj {

	int head[N], nxt[M], to[M], ne, n;

	void init(int n) {

		memset(head, -1, n * sizeof(head[0]));

		ne = 0;

		this->n = n;

	}

	void addEdge(int f, int t) {

		nxt[ne] = head[f];

		to[ne] = t;

		head[f] = ne++;

	}

	void addBiEdge(int f, int t) {

		addEdge(f, t);

		addEdge(t, f);

	}

	int addNode() {

		head[++n] = 0;

		return n;

	}

} adj;







int dp[N];



int dfs(int u){

    int &ret = dp[u];

    if(~ret)    return dp[u];



    ret = 0;

    neig(adj, u, e, v)

        ret = max(ret, dfs(v));



    ret++;



    return ret;

}





int n, m, u, v;



void go(int tc){

    cin >> n >> m;

    adj.init(n);

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

        cin >> u >> v;  u--, v--;

        adj.addEdge(u, v);

    }



    memset(dp, -1, sizeof dp);



    int ans = 0;

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

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



    cout << ans - 1;

}



int main(){

    init();



    int t, tc = 1;  t = 1;//cin >> t;

    while(t--)  go(tc++);



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

#include <climits>

#include <cstring>

#include <vector>

using namespace std;

typedef long long int ll;

const ll inf = 1000000000000;

const ll maxn = 100010;

vector <ll> adj[maxn];

ll dp[maxn];



ll dfs(ll p){

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

    ll maxpath = 0;

    for(auto x: adj[p]){

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

    }

    return dp[p] = maxpath;

}

int main(){

    memset(dp,-1,sizeof dp);



    ll n,k;



    scanf("%lld %lld",&n,&k);



    for(ll i = 0; i < k; i++){

        ll a,b;

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

        adj[a].push_back(b);

    }

    ll ans = -1;

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

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

    }

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

}


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



using namespace std;



#define F first

#define S second

#define modulo ll (1e9 + 7)



typedef long long ll;

typedef pair<int,int> pii;



void init(){

    cin.tie(0);

    cin.sync_with_stdio(0);

}



const int N = 1e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;

const ll llOO = 0x3f3f3f3f3f3f3f3f;





#define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])



struct Adj {

	int head[N], nxt[M], to[M], ne, n;

	void init(int n) {

		memset(head, -1, n * sizeof(head[0]));

		ne = 0;

		this->n = n;

	}

	void addEdge(int f, int t) {

		nxt[ne] = head[f];

		to[ne] = t;

		head[f] = ne++;

	}

	void addBiEdge(int f, int t) {

		addEdge(f, t);

		addEdge(t, f);

	}

	int addNode() {

		head[++n] = 0;

		return n;

	}

} adj;







int dp[N];



int dfs(int u){

    int &ret = dp[u];

    if(~ret)    return dp[u];



    ret = 0;

    neig(adj, u, e, v)

        ret = max(ret, dfs(v));



    ret++;



    return ret;

}





int n, m, u, v;



void go(int tc){

    cin >> n >> m;

    adj.init(n);

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

        cin >> u >> v;  u--, v--;

        adj.addEdge(u, v);

    }



    memset(dp, -1, sizeof dp);



    int ans = 0;

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

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



    cout << ans - 1;

}



int main(){

    init();



    int t, tc = 1;  t = 1;//cin >> t;

    while(t--)  go(tc++);



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

#include<queue>

#include<vector>

#define ii pair<int,int>

#define iii pair<int,ii> 

using namespace std;

int v,e,a,b;

vector<int> adjl[110000];

int vis[110000];

int memo[110000];

int start = 0;

int dp(int nodes){

//	cout<<nodes<<"\n";

	int sz = adjl[nodes].size();

	if(sz == 0) return 0;

	if(memo[nodes]!=-1) return memo[nodes];

	int ret = 0;

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

//		cout<<" visit "<<adjl[nodes][i]<<"\n";

		ret = max(ret, dp(adjl[nodes][i]) + 1);

	}

//	cout<<"\n";

	return memo[nodes] = ret;

}





int main(){

	cin>>v>>e;

	for(int i = 1;i<=v;i++) adjl[0].push_back(i);

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

		cin>>a>>b;

		adjl[a].push_back(b);

	}

	int ans = 0;

	for(int i = 0;i<=v;i++) memo[i] = -1;

	ans = dp(0) -1 ;

	cout<<ans<<"\n";

//	for(int i = 0;i<=v;i++) cout<<memo[i]<<" ";cout<<"\n";

//	for(int i = 0;i<=v;i++) ans = max(ans,)

	return 0;

}

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



using namespace std;



#define F first

#define S second

#define modulo ll (1e9 + 7)



typedef long long ll;

typedef pair<int,int> pii;



void init(){

    cin.tie(0);

    cin.sync_with_stdio(0);

}



const int N = 1e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;

const ll llOO = 0x3f3f3f3f3f3f3f3f;





#define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])



struct Adj {

	int head[N], nxt[M], to[M], ne, n;

	void init(int n) {

		memset(head, -1, n * sizeof(head[0]));

		ne = 0;

		this->n = n;

	}

	void addEdge(int f, int t) {

		nxt[ne] = head[f];

		to[ne] = t;

		head[f] = ne++;

	}

	void addBiEdge(int f, int t) {

		addEdge(f, t);

		addEdge(t, f);

	}

	int addNode() {

		head[++n] = 0;

		return n;

	}

} adj;







int dp[N];



int dfs(int u){

    int &ret = dp[u];

    if(~ret)    return dp[u];



    ret = 0;

    neig(adj, u, e, v)

        ret = max(ret, dfs(v));



    ret++;



    return ret;

}





int n, m, u, v;



void go(int tc){

    cin >> n >> m;

    adj.init(n);

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

        cin >> u >> v;  u--, v--;

        adj.addEdge(u, v);

    }



    memset(dp, -1, sizeof dp);



    int ans = 0;

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

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



    cout << ans - 1;

}



int main(){

    init();



    int t, tc = 1;  t = 1;//cin >> t;

    while(t--)  go(tc++);



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

using namespace std;

const int N = 1e5 + 10;

int n, m, dp[N];

vector<int> gg[N];

int dfs(int v){

	if (gg[v].size() == 0 || dp[v] != 0){

		return dp[v];

	}

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

		int child = gg[v][i];

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

	}

	return dp[v];

}

int main(){

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

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

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	// cout << setprecision(10) << fixed;

	cin>>n>>m;

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

		int from, to;

		cin>>from>>to;

		gg[from].push_back(to);

	}

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

		dfs(i);

	}

	int ans = 0;

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

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

	}

	cout<<ans;

}

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



using namespace std;



#define F first

#define S second

#define modulo ll (1e9 + 7)



typedef long long ll;

typedef pair<int,int> pii;



void init(){

    cin.tie(0);

    cin.sync_with_stdio(0);

}



const int N = 1e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;

const ll llOO = 0x3f3f3f3f3f3f3f3f;





#define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])



struct Adj {

	int head[N], nxt[M], to[M], ne, n;

	void init(int n) {

		memset(head, -1, n * sizeof(head[0]));

		ne = 0;

		this->n = n;

	}

	void addEdge(int f, int t) {

		nxt[ne] = head[f];

		to[ne] = t;

		head[f] = ne++;

	}

	void addBiEdge(int f, int t) {

		addEdge(f, t);

		addEdge(t, f);

	}

	int addNode() {

		head[++n] = 0;

		return n;

	}

} adj;







int dp[N];



int dfs(int u){

    int &ret = dp[u];

    if(~ret)    return dp[u];



    ret = 0;

    neig(adj, u, e, v)

        ret = max(ret, dfs(v));



    ret++;



    return ret;

}





int n, m, u, v;



void go(int tc){

    cin >> n >> m;

    adj.init(n);

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

        cin >> u >> v;  u--, v--;

        adj.addEdge(u, v);

    }



    memset(dp, -1, sizeof dp);



    int ans = 0;

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

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



    cout << ans - 1;

}



int main(){

    init();



    int t, tc = 1;  t = 1;//cin >> t;

    while(t--)  go(tc++);



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

#include <vector>

#include <queue>



using namespace std;

struct snod

{

    vector <int> v;

    int len,leg;

} nod[100005];

int n,m,i,x,y,maxi;

queue <int> q;

int main()

{

    cin>>n>>m;

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

    {

        cin>>x>>y;

        nod[x].v.push_back(y);

        nod[y].leg++;

    }

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

        if(!nod[i].leg)

        {

            q.push(i);

        }



    while(!q.empty())

    {

        int poz=q.front();

        for(int it : nod[poz].v)

        {

            nod[it].len=max(nod[it].len, nod[poz].len+1);

            nod[it].leg--;

            if(!nod[it].leg)

                q.push(it);

        }

        q.pop();

    }

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

    {

        maxi=max(maxi, nod[i].len);

    }

    cout<<maxi;

    return 0;

}


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



using namespace std;



#define F first

#define S second

#define modulo ll (1e9 + 7)



typedef long long ll;

typedef pair<int,int> pii;



void init(){

    cin.tie(0);

    cin.sync_with_stdio(0);

}



const int N = 1e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;

const ll llOO = 0x3f3f3f3f3f3f3f3f;





#define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])



struct Adj {

	int head[N], nxt[M], to[M], ne, n;

	void init(int n) {

		memset(head, -1, n * sizeof(head[0]));

		ne = 0;

		this->n = n;

	}

	void addEdge(int f, int t) {

		nxt[ne] = head[f];

		to[ne] = t;

		head[f] = ne++;

	}

	void addBiEdge(int f, int t) {

		addEdge(f, t);

		addEdge(t, f);

	}

	int addNode() {

		head[++n] = 0;

		return n;

	}

} adj;







int dp[N];



int dfs(int u){

    int &ret = dp[u];

    if(~ret)    return dp[u];



    ret = 0;

    neig(adj, u, e, v)

        ret = max(ret, dfs(v));



    ret++;



    return ret;

}





int n, m, u, v;



void go(int tc){

    cin >> n >> m;

    adj.init(n);

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

        cin >> u >> v;  u--, v--;

        adj.addEdge(u, v);

    }



    memset(dp, -1, sizeof dp);



    int ans = 0;

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

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



    cout << ans - 1;

}



int main(){

    init();



    int t, tc = 1;  t = 1;//cin >> t;

    while(t--)  go(tc++);



}