// Slower C++ solution:
#include <iostream>

#include <vector>

#include <queue>



using namespace std;

struct snod

{

    vector <int> v;

    int len,leg;

} nod[100005];

int n,m,i,x,y,maxi;

queue <int> q;

int main()

{

    cin>>n>>m;

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

    {

        cin>>x>>y;

        nod[x].v.push_back(y);

        nod[y].leg++;

    }

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

        if(!nod[i].leg)

        {

            q.push(i);

        }



    while(!q.empty())

    {

        int poz=q.front();

        for(int it : nod[poz].v)

        {

            nod[it].len=max(nod[it].len, nod[poz].len+1);

            nod[it].leg--;

            if(!nod[it].leg)

                q.push(it);

        }

        q.pop();

    }

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

    {

        maxi=max(maxi, nod[i].len);

    }

    cout<<maxi;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.220577675725437x):
#pragma region

#include <algorithm>

#include <cstring>

#include <iostream>

#include <map>

#include <queue>

#include <stack>

#include <string>

#include <vector>

typedef long long ll;

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define rep(i, a, n) for (long long i = a; i <= n; ++i)

#define per(i, a, n) for (long long i = n; i >= a; --i)

#define IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

template <typename T>

void read(T &x) {

    x = 0;

    char ch = getchar();

    ll f = 1;

    while (!isdigit(ch)) {

        if (ch == '-') f *= -1;

        ch = getchar();

    }

    while (isdigit(ch)) {

        x = x * 10 + ch - 48;

        ch = getchar();

    }

    x *= f;

}

using namespace std;

/*

 *                                                     __----~~~~~~~~~~~------___

 *                                    .  .   ~~//====......          __--~ ~~

 *                    -.            \_|//     |||\\  ~~~~~~::::... /~

 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-

 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~

 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /

 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /

 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /

 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\

 *           '         ~-|      /|    |-~\~~       __--~~

 *                       |-~~-_/ |    |   ~\_   _-~            /\

 *                            /  \     \__   \/~                \__

 *                        _--~ _/ | .-~~____--~-/                  ~~==.

 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||

 *                                  -_     ~\      ~~---l__i__i__i--~~_/

 *                                  _-~-__   ~)  \--______________--~~

 *                                //.-~~~-~_--~- |-------~~~~~~~~

 *                                       //.-~~~--\

 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 */



#pragma endregion

const int maxn = 1e5 + 5;

int n, m;

vector<int> g[maxn];

int dp[maxn], in[maxn];

int main() {

    IO;

    cin >> n >> m;

    while (m--) {

        int x, y;

        cin >> x >> y;

        g[x].push_back(y);

        in[y]++;

    }

    queue<int> q;

    rep(i, 1, n) if (!in[i]) q.push(i);

    int ans = 0, sz = q.size();

    while (q.size()) {

        int now = q.front();

        q.pop();

        for (auto nex : g[now]) {

            in[nex]--;

            if (!in[nex])

                q.push(nex);

        }

        --sz;

        if (!sz) {

            sz = q.size();

            ans++;

        }

    }

    cout << ans - 1 << endl;

}