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

#include <stdio.h>

#include <string>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>

#include <set>

#include <map>

#include <unordered_map>

#include <queue>

#include <deque>

#include <stack>

#include <bitset>

#include <iomanip>



#define ll long long

#define ld long double

#define ull unsigned long long

#define endl "\n"

#define MohamedMotaz ios::sync_with_stdio(0); cin.tie(0); ios_base::sync_with_stdio(0);

#define f(a, b, c) for(int a = b; a < c; a++)

#define fr(a, b, c) for(int a = b; a >= c; a--)

using namespace std;





//ll fact[1000001]; ll inv[1000001];

//ll primes[100007];

//ll arr[1000007];

//ll modPower(ll b, ll p) {

//	if (p == 0)

//		return 1;

//

//	ll halfpow = modPower(b, p / 2);

//	ll toReturn = (halfpow * halfpow) % mod;

//	if (p % 2)

//		toReturn = (toReturn * b) % mod;

//

//	return toReturn;

//}

//

//ll fastPower(ll b, ll p) {

//	if (p == 0)

//		return 1;

//	ll ans = fastPower(b, p / 2);

//	ans = (ans * ans);

//	if (p % 2 != 0)

//		ans = (ans * b);

//	return ans;

//}

//ll GcdRecursive(ll a, ll b) {

//	if (b == 0) return a;

//	return GcdRecursive(b, a % b);

//}

//ll modLCM(ll a, ll b) {

//	ll val = GcdRecursive(a, b);

//	ll tmp = ((a % mod) * (b % mod)) % mod;

//	ll finalVal = ((tmp % mod) * (arr[val] % mod)) % mod;

//	return finalVal;

//

//}

//ll LCM(ll a, ll b) {

//	return (a * b) / GcdRecursive(a, b);

//}

//void move1step(ll& a, ll& b, ll q) { // a and b by reference

//	ll c = a - q * b;

//	a = b;

//	b = c;

//}

//ll GcdIterative(ll a, ll b) {

//	while (b) move1step(a, b, a / b);

//	return a;

//}

//

//void pre(ll n) {

//

//	fact[0] = 1;

//	inv[0] = 1;

//

//	for (ll i = 1; i <= n; i++) {

//		fact[i] = (i * fact[i - 1]) % mod;

//		inv[i] = modPower(fact[i], mod - 2);

//		arr[i] = modPower(i, mod - 2);

//	}

//}

//

//ll npr(ll n, ll r) {

//	return ((fact[n] * inv[n - r]) % mod);

//}

//

//ll ncr(ll n, ll r) {

//	return ((((fact[n] * inv[n - r]) % mod) * inv[r]) % mod);

//}

//

//void sieve(ll val) {

//	memset(primes, 1, sizeof primes);

//	primes[0] = primes[1] = false;

//	for (int i = 2; i <= val; i++) {

//		if (primes[i]) {

//			for (int j = i * i; j <= val; j += i) {

//				primes[j] = 0;

//			}

//		}

//	}

//

//}



const ll mod = 1e9 + 7;

const ll N = 2e5 + 7;

const ll inf = 1e9 + 5;







ll n,m, a, b, ans = 0, c= 0;

vector<ll> adj[N];

bool visited[N] = {false};

ll dp[N] = {};

void dfs(ll node){





    visited[node] = true;

    for (auto elem: adj[node]){

        if (!visited[elem]) dfs(elem);

        dp[node] = max(dp[node], dp[elem] + 1);

        ans = max(ans, dp[node]);

    }





}



int main()

{

    MohamedMotaz;

    cin >> n >> m;

    f(i,0,m){

        cin >> a >> b;

        adj[a].push_back(b);

    }



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



    cout << ans;

}








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

#define REP(x,y,z) for(int x=y;x<=z;x++)

#define MSET(x,y) memset(x,y,sizeof(x))

#define M 100005

using namespace std;

int n,m,dp[M];

bool vis[M];

vector<int> e[M];

int dfs(int cur) {

    if (vis[cur]) {

        return dp[cur];

    }



    int &res = dp[cur];

    for (int i:e[cur]) res = max(res, 1+dfs(i));

    vis[cur] = true;

    return res;

}

int main()

{

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

    REP(i,1,m) {

        int x,y;

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

        e[x].push_back(y);

    }



    int ans = 0;

    REP(i,1,n) ans = max(ans, dfs(i));

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

    return 0;

}