// Slower C++ solution:
#include<set>

#include<map>

#include<cmath>

#include<ctime>

#include<queue>

#include<stack>

#include<cstdio>

#include<string>

#include<vector>

#include<random>

#include<cstdlib>

#include<cstring>

#include<iomanip>

#include<iostream>

#include<algorithm>

#include<unordered_map>

#define endl "\n"

#define fi first

#define se second

#define db double

#define gcd __gcd

#define pb push_back

#define mp make_pair

#define lowbit(x) (x & (-x))

#define PII  pair<int, int> 

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

#define debug(x) cout << #x << " = " << x << endl

#define rep(i, a, b) for(__typeof(b) i = a; i <= (b); i++)

#define Rep(i, a, b) for(__typeof(a) i = a; i >= (b); i--)

#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

template<class T> inline T qmin(T a, T b) { return a < b ? a : b; }

template<class T> inline T qmax(T a, T b) { return a > b ? a : b; }

typedef long long ll;

typedef unsigned long long ull;

const db eps = 1e-9;

const db PI = acos(-1);

const int inf = 0x3f3f3f3f;

const int mod = (int)1e9 + 7;

const int maxn = (int)1e5 + 5;//remember to modify it, No RE or MLE

const ll INF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

ll qpow(ll x, ll y) { ll ret = 1; for( ; y; y >>= 1, x = (x * x) % mod) if(y & 1) ret = (ret * x) % mod; return ret; }



inline int read(){

	char ch = getchar();

	int x = 0, f = 1;

	while(ch < '0' || ch > '9'){

		if(ch == '-') f = -1;

		ch = getchar();

	}

	while('0' <= ch && ch <= '9'){

		x = x * 10 + ch - '0';

		ch = getchar();

	}

	return x * f;

}



int n, m;

vector<int> G[maxn];

int dp[maxn], ind[maxn];



void dfs(int u){

	for(int v : G[u]){

		if(!dp[v]){

			dfs(v);

			dp[u] = qmax(dp[u], dp[v] + 1);

		} else {

			dp[u] = qmax(dp[u], dp[v] + 1);

		}

	}

}



int main()

{

	n = read(), m = read();

	rep(i, 1, m) {

		int u = read(), v = read();

		G[u].pb(v);

		ind[v]++;

	}

	rep(i, 1, n){

		if(!ind[i]){

			dfs(i);

		}

	}

	printf("%d\n", *max_element(dp, dp + n + 1));

	return 0;

}

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



}