//参照AC
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
typedef long long LL;
typedef long double Lf;
#define Rep(i,a,n)for(int i=(int)(a);i<(int)(n);i++)
//#define Rep(i,a,n)for(LL i=(LL)(a);i<(LL)(n);i++)
#define rep(i,n)Rep(i,0,n)
#define Repp(i,l,r,k)for(int i=(int)(l);i<(int)(r);i+=(int)(k))
//#define Repp(i,l,r,k)for(LL i=(LL)(l);i<(LL)(r);i+=(LL)(k))
#define Ma(x,y) ((x)>(y)?(x):(y))
#define mi(x,y) ((x)<(y)?(x):(y))
#define MOD 1000000007//998244353,1000000007,1000000009,4999999999999997
int upi(const void* a, const void* b) { return*(int*)a<*(int*)b ? -1 : *(int*)a> * (int*)b ? 1 : 0; }
int downi(const void* a, const void* b) { return*(int*)a<*(int*)b ? 1 : *(int*)a> * (int*)b ? -1 : 0; }
void sortupi(int* a, int n) { qsort(a, n, sizeof(int), upi); } //1,2,3,4,...
void sortdowni(int* a, int n) { qsort(a, n, sizeof(int), downi); }
int upLL(const void* a, const void* b) { return*(LL*)a<*(LL*)b ? -1 : *(LL*)a> * (LL*)b ? 1 : 0; }
int downLL(const void* a, const void* b) { return*(LL*)a<*(LL*)b ? 1 : *(LL*)a> * (LL*)b ? -1 : 0; }
void sortupLL(LL* a, int n) { qsort(a, n, sizeof(LL), upLL); }
void sortdownLL(LL* a, int n) { qsort(a, n, sizeof(LL), downLL); }
int cmp_char(const void* a, const void* b) { return *(char*)a - *(char*)b; } // a,b,c,d
int cmp_str(const void* a, const void* b) { return strcmp(*(const char**)a, *(const char**)b); } // aaa,aab
LL gcd(LL p, LL q) { while (q) { LL t = p % q; p = q; q = t; }return p; }
LL modpow(LL a,LL n,int m){LL x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invmodpow(b,p)modpow(b,p-2,p)// a/b≡a*b^{-1}(mod p)...return b^{-1}(mod p)
typedef struct fac { LL n, exp; }F;
LL fact(LL n,F*a){LL x,i,j,c=0;x=n;for(i=2;i*i<=x;i++){for(j=1;x%i==0;j++)x/=i;if(j>1){a[c].n=i;a[c++].exp=j-1;}}if(x-1){a[c].n=x;a[c++].exp=1;}return c;}

//snippet


#define N 1009


typedef struct directedGraph {
	int* vertex;
	int* next;
	int* start;
	int v, e;
	int pointer;
} graph;

graph* newGraph(const int v, const int e) {
	graph* g = (graph*)malloc(sizeof(graph));
	g->vertex = (int*)calloc(e, sizeof(int));
	g->next = (int*)calloc(e, sizeof(int));
	g->start = (int*)calloc(v, sizeof(int));
	rep(i, v) g->start[i] = -1;
	g->v = v;
	g->e = e;
	g->pointer = 0;
	return g;
}

void freeGraph(graph* g) {
	free(g->vertex);
	free(g->next);
	free(g->start);
	free(g);
	return;
}

void addEdge(graph* g, const int from, const int to) {
	const int p = g->pointer;
	g->vertex[p] = to;
	g->next[p] = g->start[from];
	g->start[from] = p;
	g->pointer++;
	return;
}

void run() {
	int n, m;
	scanf("%d%d", &n, &m);
	graph* g = newGraph(n, m); int q = m;
	while (m--) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--; y--;
		addEdge(g, x, y);
	}
	
	int* dp = (int*)calloc(n, sizeof(int));
	int* stack = (int*)calloc(2 * n, sizeof(int));
	int ans = 0;
	rep(i, n) {
		int top = 0;
		stack[top++] = i;
		while (top > 0) {
			int v = stack[--top];
			if (v < n && dp[v]) continue;
			if (v >= n) {
				v -= n;
				int local = 1;
				for (int p = g->start[v]; p != -1; p = g->next[p]) {
					int u = g->vertex[p];
					local = Ma(local, dp[u] + 1);
				}
				dp[v] = local;
			}
			else {
				stack[top++] = v + n;
				for (int p = g->start[v]; p != -1; p = g->next[p]) {
					int u = g->vertex[p];
					stack[top++] = u;
				}
			}
		}
		ans = Ma(ans, dp[i] - 1);
	}
	printf("%d\n", ans);
	freeGraph(g);
}

int main() {
	int t = 1;
	while (t--) {
		run();
	}
	return 0;
}

Measured memory_bytes: 3104