
function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});    
	let N = parseInt(input[0][0],10);
	let M = parseInt(input[0][1],10);
	let arr_xy = [];
	graph = Array.from({length: N}, () => []);
	for (let i = 0; i < M; i++){
		arr_xy.push(input[i + 1].map(e => parseInt(e, 10)));
		graph[arr_xy[i][0] - 1].push(arr_xy[i][1] - 1);
	}
	v = Array.from({length: N}, () => -1);
	let ans = 0;
	for (let i = 0; i < N; i++){
		ans = Math.max(ans, DP(i));
	}
	console.log(ans);
}

function DP(x){
	if (v[x] !== -1) {
		return v[x];
	} else {
		let maxD = -1;
		for (let a of graph[x]){	
			maxD = Math.max(DP(a), maxD);
		}
		v[x] = maxD + 1;
		return v[x];
	}
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));	