'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function getArray(str) {
    return str.split(' ').map(x => Number(x));
}

function getInt(str) {
    return Number(str);
}

function getStrArray(str) {
    return str.split(' ').map(x => x);
}

function create2DArray(row, col, filler = 0) {
    return Array(row)
        .fill(filler)
        .map(() => Array(col).fill(filler));
}
// ---------------------------------------



function main() {
    let [n, m] = getArray(readline());
    let g = {};
    while(m--) {
        const [x, y] = getArray(readline());
        g[x] ? g[x].push(y) : g[x] = [y];
    }

    let dp = new Array(n+1);
    dp.fill(0);

    let visited = {};

    function visit(v) {
        visited[v] = true;
        for(let i in g[v]) {
            let child = g[v][i];
            // console.log(child);
            // console.log(g, v, child, '+++++');
            if (!visited[child]) {
                visit(child);
            }
            dp[v] = Math.max(dp[v], 1 + dp[child]);
            // console.log(dp, v);
        }
    }

    // console.log(g);
    for(let i in g) {
        if (!visited[i]) {
            visit(i);
        }
    }

    let ans = 0;
    for (let i in dp) {
        ans = Math.max(dp[i], ans);
    }
    console.log(ans);
}