import std.stdio;

void main() {
	uint vertex, side;
	readf(" %s %s", &vertex, &side);

	auto direction = new uint[][vertex];

	foreach (i;0..side) {
		uint v0, v1;
		readf(" %s %s", &v0, &v1);
		--v0;
		--v1;
		direction[v0] ~= v1;
	}

	auto dp = new int[vertex];
	dp[] = -1;

	int maxPath(uint v) {
		auto dir = direction[v];

		if (dp[v] != -1) {
			return dp[v];
		}

		if (dir.length == 0) {
			dp[v] = 0;
			return 0;
		}

		uint m;
		foreach (d;dir) {
			auto t = maxPath(d);
			if (m < t) {
				m = t;
			}
		}
		++m;
		dp[v] = m;
		return m;
	}

	int m;
	foreach (i;0..vertex) {
		int t = maxPath(i);
		if (m < t) {
			m = t;
		}
	}
	writeln(m);
}
