// 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 dp[100100];

int n,m;

vector<int> ga[100100];

int u,v;

int d(int x){

	if(dp[x]!=-1)

		return dp[x];

	int &res=dp[x];

	res=0;

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

		int y=ga[x][i];

		res=max(res,d(y)+1);

	}

	return res;

}

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

	}

	int res=0;

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

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

		res=max(res,d(i));

	}

	cout<<res<<endl;

	return 0;

}


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

using namespace std;

 

#define             ll  long long

#define            ull  unsigned long long

#define            P_B  push_back

#define            M_P  make_pair

#define         DBG(x)  printf("%s %d\n", #x, x)

#define       DBGLL(x)  printf("%s %lld\n", #x, x)

#define         SCI(x)  scanf("%d",&x)

#define         SCD(x)  scanf("%lf",&x)

#define         SCS(x)  scanf("%s",x)

#define         CLR(x)  memset(x, 0, sizeof(x))

#define        CLRN(x)  memset(x, -1, sizeof(x))

#define        SCLL(x)  scanf("%lld", &x)

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

#define  FORR(i, m, n)  for(int i = m; i < n; i++)

#define     SCI2(m, n)  scanf("%d%d", &m, &n)

#define  SCI3(m, n, o)  scanf("%d%d%d",&m, &n, &o)

#define     SCD2(m, n)  scanf("%lf%lf",&m,&n)

#define  SCD3(m, n, o)  scanf("%lf%lf%lf",&m,&n,&o)

#define    SCLL2(m, n)  scanf("%lld%lld", &m, &n)

#define SCLL3(m, n, o)  scanf("%lld%lld%lld",&m, &n, &o)

#define             vi  vector<int>

#define            vii  vector<pair<int, int> >

#define            vll  vector<pair<ll, ll> >

#define            Mod  1000000007

#define            INF  1000000000

 



int n, m;

queue<int> st;

vi node[100001];

vector<bool> selected(100001, false);

vector<bool> visited(100001, false);

int maxxes[100001];



void topsort(int v)

{

	if(selected[v]) return;

	selected[v] = true;

	for(auto it : node[v]){

		if(!selected[it]){

			topsort(it);

		}

	}

	st.push(v);

}



int notdfs(int v, int cnt)

{

	// cnt++;

	visited[v] = true;

	int mx =  -1;

	// DBG(mx);

	// DBG(v);

	for(auto it : node[v]){

		// if(!visited[it]){

			mx = max(mx, maxxes[it]);

			// DBG(mx);

			// DBG(it);

		// }

	}

	maxxes[v] = mx + 1;

	return mx + 1;

}



int main()

{

	SCI2(n, m);

	FORR(i, 1, m + 1){

		int p, q;

		SCI2(p, q);

		node[p].P_B(q);

	}

	FORR(i, 1, n + 1)topsort(i);

	int max_ = 0;

	FORR(i, 1, n + 1){

		int x = st.front();

		// DBG(x);

		st.pop();

		if(!visited[x]){

			max_ = max(max_, notdfs(x, -1));

		}

	}

	// FORR(i, 1, n + 1) printf("%d ", maxxes[i]);

	// cout << endl;

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

}