// Slower C++ solution:
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long

#define ull unsigned long long

#define db long double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

#define sz(x) (int)(x).size()

#define all(x) (x).begin(), (x).end()

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



//#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair <int, int> pii;



const int N = 1e5 + 7;

int n, m, dp[N];

vector <int> q[N], r[N], ord;

bool used[N];



void dfs(int v) {

    used[v] = true;

    for (auto i : q[v]) {

        if (!used[i]) {

            dfs(i);

        }

    }

    ord.pb(v);

}



void solve(int tc) {

    // check for (int i = 0; i < n; j++)

    cin >> n >> m;

    for (int i = 1; i <= m; i++) {

        int x, y;

        cin >> x >> y;

        q[x].pb(y);

        r[y].pb(x);

    }

    for (int i = 1; i <= n; i++) {

        if (!used[i]) {

            dfs(i);

        }

    }

    reverse(all(ord));

    int res = 0;

    for (int i = 1; i <= n; i++) {

        int v = ord[i - 1];

        for (auto j : r[v]) {

            dp[v] = max(dp[v], dp[j] + 1);

        }

        res = max(res, dp[v]);

    }

    cout << res;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

//    freopen("input.txt", "r", stdin);

//    freopen("output.txt", "w", stdout);

    int tc = 1;

//    cin >> tc;

    for (int i = 0; i < tc; i++) {

        solve(i);

//        cleanup();

    }

}


// Faster C++ solution for the same problem (measured speedup: 1.825546947161674x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

const int maxn = 1e5 + 9;

inline int read()

{

    int data = 0, f = 1;

    char ch = getchar();

    while (!isdigit(ch))

    {

        if (ch == '-')

            f = -1;

        ch = getchar();

    }

    while (isdigit(ch))

    {

        data = (data << 3) + (data << 1) + ch - '0';

        ch = getchar();

    }

    return f * data;

}

vector<int> edge[maxn];

int dis[maxn], ans;

bool vis[maxn];

void dfs(int x)

{

    if (vis[x])

        return;

    vis[x] = true;

    for (int i = 0; i < edge[x].size(); ++i)

    {

        dfs(edge[x][i]);

        dis[x] = max(dis[x], dis[edge[x][i]] + 1);

    }

    ans = max(ans, dis[x]);

}

int main()

{

    //freopen("in.txt","r",stdin);

    //freopen("data.txt","w",stdout);

    //std::ios::sync_with_stdio(false);

    //std::cin.tie(0);

    int n = read(), m = read();

    while (m--)

    {

        int x = read(), y = read();

        edge[x].emplace_back(y);

    }

    for (int i = 1; i <= n; ++i)

        dfs(i);

    cout << ans << '\n';

    //fclose(stdin);

    //fclose(stdout);

    return 0;

}