#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;

/***** type *****/
using ll = long long;
using ld = long double;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
template <class T1, class T2> using umap = unordered_map<T1, T2>;
using ml = boost::multiprecision::cpp_int;

/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define val(itr) *(itr)                          // get value
#define f first                                  // pair first
#define s second                                 // pair second
/***** const value *****/
#define llong_max 9223372036854775807            // 9 * 10^18
#define ldbl_max 1.79769e+308                    // 1.7 * 10^308
#define pi 3.1415926535897932                    // 3.14 ...
/***** for each macro *****/
#define fori(i, ...) if(ll i = -1) for(__VA_ARGS__) if(i++, true)
#define each(i, e, c) fori(i, auto e = c.begin(); e != c.end(); ++e)
#define forir(i, v, ...) if(ll i=(ll)v.size())for(__VA_ARGS__)if(i--,true)
#define eachr(i, e, c) forir(i, auto e = c.rbegin(); e != c.rend(); ++e)

/***** lambda *****/
auto Count = [] // long long count value
(auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoN = [] // char to number
(auto c) { return (ll)(c - '0'); };
auto DivC = [] // long double div ceiling
(auto a, auto b) { return ceil((ld)a / (ld)b); };
auto Fix = [] // fix value
(auto b, auto e, auto fix)
{ for (auto it = b; it != e; ++it)*it += fix; };
auto Pow = [] // long long pow
(auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
(auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
(auto n) { return (ll)pow(10, n); };
auto Size = []  // long long collection size
(auto& c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
(auto b, auto e) { return accumulate(b, e, 0LL); };

/***** template *****/
template <class T> void MakeVVT
(ll ys, ll xs, vvt<T>& v, T fill = T())
{	// vector<vector<T>> resize + fill
	v.resize(ys); rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T> void MakeVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fill = T())
{	// vector<vector<vector<T>>> resize + fill
	v.resize(zs); rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T> void InputVT
(ll xs, vt<T>& v, T fix = T())
{	// input vector<T> (T != struct) + fix
	v.resize(xs); rep(i, 0, xs) { cin >> v[i]; v[i] += fix; }
}
template <class T> void InputVVT
(ll ys, ll xs, vvt<T>& v, T fix = T())
{	// input vector<vector<T>> (T != struct) + fix
	MakeVVT(ys, xs, v, fix);
	rep(y, 0, ys) rep(x, 0, xs) { cin >> v[y][x]; v[y][x] += fix; }
}
template <class T> void InputVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fix = T())
{	// input vector<vector<vector<T>>> (T != struct) + fix
	v.resize(zs); rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget
{
	/*** 頂点数nのグラフを頂点sからBFSで処理する ***/

	struct NyaBFS
	{
		// BFSで必ず使う変数
		vector<vector<long long>> g;
		// その他の変数
		//vector<long long> vdata;
		vector<long long> ans;                             // sから各頂点までの長さ
		unordered_map<long long, long long> ideg;          // 頂点の入次数
		vector<unordered_map<long long, long long>> edata; // 各辺の長さ

		void Run(vector<long long> s)
		{
			queue<long long> q;
			for (auto& e : s) q.push(e);

			while (!q.empty())
			{
				long long v = q.front(); q.pop();
				for (auto& e : g[v])
				{
					ideg[e]--;
					if (ideg[e] == 0)
					{	// 入次数ゼロのタイミング = 最大ステップで到達
						ans[e] = max(ans[e], ans[v] + edata[v][e]);
						q.push(e);
					}
				}
			}
		}

		void Run(long long s)
		{
			queue<long long> q;
			q.push(s);

			while (!q.empty())
			{
				long long v = q.front(); q.pop();
				for (auto& e : g[v])
				{
					// 頂点vから頂点eへ遷移するときの処理
				}
			}
		}
	};
}

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
//using mll = ModLL< 1000000007 >;

int dp[100100];
int main()
{
	ll N, M; cin >> N >> M;
	NyaBFS bfs;

	bfs.g.resize(N);
	bfs.edata.resize(N);
	bfs.ans.resize(N);
	rep(i, 0, M)
	{
		ll x, y; cin >> x >> y; x--; y--;
		bfs.g[x].push_back(y);
		bfs.edata[x][y] = 1;
		bfs.ideg[y]++;
	}

	vt<ll> s;
	rep(i, 0, N)
	{
		if (bfs.ideg[i] == 0) s.push_back(i);
	}
	bfs.Run(s);

	cout << *max_element(all(bfs.ans)) << endl;
	return 0;
}


Measured memory_bytes: 22308