#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

struct SCC {
    // T: adjacency list of topological graph
    // C: lists (scc id : the same scc nodes vec)
    vector<vector<int>> G, rG, T, C;
    vector<int> vs, cmp; // vs: order of nodes just before back track
    vector<bool> used;
    SCC() {}
    SCC(int n) : G(n), rG(n), used(n), cmp(n){};
    void add_edge(int u, int v) { // every vertex is from 0 to n-1
        G[u].emplace_back(v);
        rG[v].emplace_back(u);
    }
    void dfs(int v) {
        used[v] = true;
        for (int u : G[v]) {
            if (!used[u]) {
                dfs(u);
            }
        }
        vs.emplace_back(v);
    }
    void rdfs(int v, int k) { // v is node. k is the order of topological
        used[v] = true;
        cmp[v] = k;
        C[k].emplace_back(v);
        for (int u : rG[v]) {
            if (!used[u]) {
                rdfs(u, k);
            }
        }
    }
    int scc() {
        fill(used.begin(), used.end(), 0);
        int n = G.size();
        for (int v = 0; v < n; v++) {
            if (!used[v]) {
                dfs(v);
            }
        }
        fill(used.begin(), used.end(), 0);
        int k = 0;
        for (int i = n - 1; i >= 0; i--) { // reversed order
            if (!used[vs[i]]) {
                T.emplace_back();
                C.emplace_back();
                rdfs(vs[i], k);
                k++;
            }
        }
        for (int v = 0; v < n; v++)
            for (int u : G[v])
                if (cmp[v] != cmp[u])
                    T[cmp[v]].push_back(cmp[u]);
        for (int i = 0; i < k; i++) {
            sort(T[i].begin(), T[i].end());
            T[i].erase(unique(T[i].begin(), T[i].end()), T[i].end()); // erase duplicated node;
        }
        return k;
    }
    int operator[](int k) const { return cmp[k]; }
};

int dp[1000000] = {0};

int main() {
    int V, E;
    cin >> V >> E;
    SCC G(V);
    for (int i = 0; i < E; i++) {
        int s, t;
        cin >> s >> t;
        s--;
        t--;
        G.add_edge(s, t);
    }

    int k = G.scc();
    /*
        cout << "\nC: " << endl;
        for (auto vec : G.C) {
            for (auto v : vec) {
                cout << v << " ";
            }
            cout << endl;
        }

        cout << "\nT: " << endl;
        for (auto vec : G.T) {
            for (auto v : vec) {
                cout << v << " ";
            }
            cout << endl;
        }
        cout << endl;
    */
    dp[0] = 0;
    REP(i, k) {
        for (auto v : G.T[i]) {
            chmax(dp[v], dp[i] + 1);
        }
    }

    int ans = 0;
    REP(i, k) { chmax(ans, dp[i]); }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 24420