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

#define fi first

#define se second

#define pb push_back

#define mp make_pair

#define rep(i,n) for((i)=0;(i)<(n);(i)++)

using namespace std;

typedef long long LL;

typedef pair<int,int> pii;

const int INF=0x3f3f3f3f;

const int N=1e5+10;



inline int read(){

	int ww=1,xx=0;char ch=getchar();

	while(!isdigit(ch)){if(ch=='-') ww=-ww;ch=getchar();}

	while(isdigit(ch)){xx=(xx<<1)+(xx<<3)+(ch^48);ch=getchar();}

	return ww*xx;

}



int n,m,fr,to,cnt;

int de[N],ans;

int q[N],l,r=-1;

int head[N];

int f[N];

vector<int> ed[N];



signed main(){

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

//  freopen(".out","w",stdout);

	n=read();

	m=read();

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

		fr=read();

		to=read();

		de[to]++;

		ed[fr].pb(to);

	}

	r=-1;

	l=0;

//	for(int i=1;i<=n;i++) printf("%d ",de[i]);

	for(int i=1;i<=n;i++) if(de[i]==0) q[++r]=i;

	while(l<=r){

		int u=q[l++];

//		printf("%d ",u);

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

			int v=ed[u][i];

			de[v]--;

			if(de[v]==0) q[++r]=v;

		}

	}

//	puts("");

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

		int u=q[i];

//		printf("%d ",u);

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

			f[ed[u][j]]=max(f[ed[u][j]],f[u]+1);

	}

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

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

	return 0;

}

/*

4 5

1 2

1 3

3 2

2 4

3 4

*/

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

typedef long long ll;

using namespace std;

const int N = 1e5+10 , M = 5e5+10, OO = 0x3f3f3f3f ,  MOD = 1e9+7;

const double EPS = 1e-3;

#define M_PI 3.14159265358979323846

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

#define loop(i,n) for(int i = 0;i < int(n);i++)

#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)

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

#define pb push_back

#define F first

#define S second

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

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

typedef pair <int,int> pii ;

typedef vector < pii > vii ;

typedef vector < int > vi;

int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

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

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





int n , m ,u, v, nxt[N],to[N],head[N],ne;

int dp[N];



void init(){



    ne = 0;

    memset(head, -1 , sizeof head);

}



void addEdge(int f, int t){



    to[ne] = t;

    nxt[ne] = head[f];

    head[f] = ne++;

}



int solve(int idx){



   // if(idx == n) return 0;



    int& ret = dp[idx];

    if(~ret) return ret;

    //if(~ret) return 0;



    ret = 0;

    for (int e = head[idx]; ~e ; e = nxt[e])

        ret = max(ret, solve(to[e])+1);





    return ret;



}







int main(){





    //ios_base::sync_with_stdio(0);

    //cin.tie(0);cout.tie(0);





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

    init();



    loop(i , m){

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

        addEdge(u, v);

    }



    int mx = 0;

    memset(dp, -1 , sizeof dp);





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

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





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



    return 0;

}