// Slower C++ solution:
// È¥°É£¡Æ¤¿¨Çð! °ÑAC´ø»ØÀ´£¡

//      ¤Ø¡¡¡¡¡¡¡¡¡¡£¯|

// ¡¡¡¡/£Ü7¡¡¡¡¡¡ ¡Ï£ß/

// ¡¡ /¡¡©¦¡¡¡¡ £¯¡¡£¯								AC		AC		AC		AC

//    ©¦¡¡Z £ß,£¼¡¡£¯¡¡¡¡ /`©c						AC		AC		AC		AC

//    ©¦¡¡¡¡¡¡¡¡¡¡©c¡¡¡¡ /¡¡¡¡¡µ					AC		AC		AC		AC

// ¡¡Y¡¡¡¡¡¡¡¡¡¡`¡¡ /¡¡¡¡/  /						AC		AC		AC		AC

// ¡¡Øé¡ñ¡¡.¡¡¡ñ¡¡¡¡¡´¡¡/¡¡/						AC		AC		AC		AC

// ¡¡()¡¡ ¤Ø¡¡¡¡¡¡¡¡|¡¡£Ü¡´							AC		AC		AC		AC

// ¡¡¡¡>- ._¡¡ ¥£¡¡ ©¦ £¯£¯							AC		AC		AC		AC

// ¡¡ / ¤Ø¡¡¡¡ /¡¡/£¼| £Ü£Ü							AC		AC		AC		AC

// ¡¡ ©c_/¡¡¡¡(_£¯¡¡ ©¦£¯£¯							AC		AC		AC		AC

// ¡¡¡¡ 7¡¡¡¡¡¡¡¡¡¡¡¡¡¡|£¯							AC		AC		AC		AC

// ¡¡¡¡ £¾¨Dr£þ£þ`-¨D£ß/							AC		AC		AC		AC

//**************************************Accepted*****************************************//



#include<bits/stdc++.h>



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

#define pb push_back

#define mp make_pair

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



#define CIN_FILE "input.txt"

#define COUT_FILE "output.txt"



using namespace std;



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

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



int Read()

{

	char c;

	c=getchar();



	int tsqua=0;



	while(c!=' '&&c!='\n')

	{

		tsqua=(((tsqua<<1)<<1)<<1)+(tsqua<<1)+(int)(c-'0');



		c=getchar();

	}



	return tsqua;

}



typedef long long ll;



int n,m;

int Deg[111111],Q[111111];

vector<int> E[111111];

int Head,Tail;

int res;

int D[111111];



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

{

	ios::sync_with_stdio(false);

	

	cin>>n>>m;

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

	{

		int a,b;

		cin>>a>>b;

		

		E[a].pb(b);

		Deg[b]++;

	}

	

	for(int i=1;i<=n;i++)if(!Deg[i])Q[++Tail]=i;

	

	while(Head<Tail)

	{

		int x=Q[++Head];

		

		res=max(res,D[x]);

		

		for(int i=0;i<E[x].size();i++)Deg[E[x][i]]--;

		

		for(int i=0;i<E[x].size();i++)D[E[x][i]]=max(D[E[x][i]],D[x]+1);

		

		for(int i=0;i<E[x].size();i++)if(!Deg[E[x][i]])Q[++Tail]=E[x][i];

	}

	

	cout<<res;

	

	//printf("Time used = %.12f",(double)(clock())/CLOCKS_PER_SEC);

	return 0;

}

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

}