import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;

string my_readln() { return chomp(readln()); }

void main()
{
	auto tokens = my_readln().split();
	auto N = tokens[0].to!ulong;
	auto M = tokens[1].to!ulong;
	ulong[][2][] v;
	v.length = N;
	foreach (i; 0..M)
	{
		auto tokens2 = my_readln().split();
		auto x       = tokens2[0].to!ulong - 1;
		auto y       = tokens2[1].to!ulong - 1;
		v[y][0]     ~= x;
		v[x][1]     ~= y;
	}

	ulong[] memo;
	memo.length = N;
	ulong search(ulong pos)
	{
		ulong r;
		foreach (e; v[pos][1])
		{
			r = max(r, memo[e] == 0 ? search(e) + 1 : memo[e] + 1);
		}
		stderr.writeln("a", pos, " ", r);
		memo[pos] = r;
		return r;
	}

	ulong ans;
	foreach (i; 0..N)
	{
		if (v[i][0].length != 0) continue;

		ans = max(ans, search(i));
		stderr.writeln("b", i, " ", ans);
	}
	writeln(ans);

	stdout.flush();
}