import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf = 10L^^15;

void main() {
    int n, m;
    scan(n, m);

    auto adj = new int[][](n, 0);

    foreach (i ; 0 .. m) {
        int xi, yi;
        scan(xi, yi);
        xi--, yi--;
        adj[yi] ~= xi;
    }

    auto dp = new int[](n);
    dp[] = -1;

    int dfs(int v) {
        if (dp[v] != -1) {
            return dp[v];
        }
        dp[v] = 0;
        foreach (u ; adj[v]) {
            dp[v] = max(dp[v], dfs(u) + 1);
        }
        return dp[v];
    }

    int ans;
    foreach (i ; 0 .. n) {
        ans = max(dfs(i), ans);
    }
    writeln(ans);
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}