// Original C++ solution (slower):
#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++);



}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f

#define INF 0x3f3f3f3f

#define pb push_back

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define ms(a, x) memset(a, x, sizeof(a))

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

#define for1(i, n) for(int i = 1; i <= n; ++i)

using namespace std;

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<long long, int> pli;

typedef pair<long long, long long> pll;

const int mod = 1e9 + 7;

const int N = 1e5+10;

const int M = 1e6+10;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

inline ll Pow(ll a, ll b) { ll ans = 1; for( ; b; b >>= 1) { if(b&1) ans = ans * a % mod; a = a * a % mod;} return ans;}

inline ll Mul ( ll a, ll b)  { ll lf = a * ( b >> 25ll ) % mod * ( 1LL << 25 ) % mod; ll rg = a * ( b & ( ( 1ll << 25 ) - 1 ) ) % mod; return ( lf + rg ) % mod;}

inline ll C(ll y, ll x) { ll ans = 1; if(y < 0 || x < 0 || y < x) return 0; y %= mod; if(!y || !x) return 1; for0(i, x) ans = ans * (y-i) % mod; for1(i, x) ans = ans * Pow(i, mod-2) % mod; return ans; }

//==================================================================================================================================================================//



int n, m, tot, head[N], nex[N], ver[N];

int dp[N], deg[N], ans;

queue<int> q;



void add(int x, int y) {

   ver[++tot] = y, nex[tot] = head[x], head[x] = tot;

}



int main() {

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

   for1(i, m) {

      int x, y;

      scanf("%d %d", &x, &y);

      add(x, y);

      deg[y]++;

   }

   for1(i, n) if(!deg[i]) q.push(i);

   while(!q.empty()) {

      int x = q.front(); q.pop();

      for(int i = head[x]; i; i = nex[i]) {

         int y = ver[i]; deg[y]--;

         dp[y] = max(dp[y], dp[x] + 1);

         if(!deg[y]) q.push(y);

      }

   }

   for1(i, n) ans = max(ans, dp[i]);

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

   return 0;

}

// Fastest known solution for this problem: