#include <bits/stdc++.h>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <utility>

#define MOD 1000000007
#define INF 1e9 + 7

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define F first
#define S second
#define pb push_back
#define all(p) p.begin(), p.end()
#define isOdd(n) n % 2
#define isEven(n) (n % 2 == 0)
#define sz(x) x.size()
#define deb(x) cout << #x << " : " << x << endl;

using namespace std;

typedef long long int lli;

typedef pair<lli, lli> pii;
typedef vector<lli> vi;
typedef set<lli> si;
typedef unordered_map<lli, lli> umii;
typedef unordered_set<lli> usi;

lli mod = 1000000007;

void dispvec(vi);
struct CustomCompare;
lli iters = 0;
//-------------------------------------SOLVE HERE-------------------------------------
int longestPath(int start, vector<vector<int>> graph, vector<int> &dp)
{
    iters++;
    if (sz(graph[start]) == 0) //sz = size of
    {
        dp[start] = 0;
        return 0;
    }
    if (dp[start] != -1)
    {
        return dp[start];
    }

    int high = 0;
    for (int i = 0; i < sz(graph[start]); i++)
    {
        high = max(high, longestPath(graph[start][i], graph, dp) + 1);
    }
    dp[start] = high;
    return high;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n + 1, vector<int>(0, {}));
    for (int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        graph[x].pb(y);
    }

    vector<int> dp(n + 1, -1);
    for (int i = 1; i <= n; i++)
    {
        if (dp[i] == -1)
        {
            dp[i] = longestPath(i, graph, dp);
        }
    }
    int high = 0;
    for (int i = 1; i <= n; i++)
    {
        high = max(high, dp[i]);
    }
    cout << high << endl;
    cout << iters << endl;
}

class Solution
{
    int n, m;
    vector<vector<int>> graph;
    vector<int> dp;

public:
    Solution()
    {
        cin >> n >> m;
        for (int i = 0; i <= n; i++)
        {
            graph.pb({});
            dp.pb(-1);
        }
        for (int i = 0; i < m; i++)
        {
            int x, y;
            cin >> x >> y;
            graph[x].pb(y);
        }
    }
    void solve()
    {
        for (int i = 1; i <= n; i++)
        {
            if (dp[i] == -1)
            {
                dp[i] = longestPath(i);
            }
        }
        int high = 0;
        for (int i = 1; i <= n; i++)
        {
            high = max(high, dp[i]);
        }
        cout << high << endl;
        // cout << iters << endl;
    }
    int longestPath(int start)
    {
        iters++;
        if (sz(graph[start]) == 0) //sz = size of
        {
            dp[start] = 0;
            return 0;
        }
        if (dp[start] != -1)
        {
            return dp[start];
        }

        int high = 0;
        for (int i = 0; i < sz(graph[start]); i++)
        {
            high = max(high, longestPath(graph[start][i]) + 1);
        }
        dp[start] = high;
        return high;
    }
};

//-------------------------------------MAIN HERE--------------------------------------

int main()
{
#ifndef ONLINE_JUDGE
    freopen("/home/mufaddal/Mufaddal/Programs/Competitive/input", "r", stdin);
    freopen("/home/mufaddal/Mufaddal/Programs/Competitive/output", "w", stdout);
#endif
    int t = 1;
    // cin >> t;
    int i = 1;
    while (i <= t)
    {
        // cout << "Case #" << i << ": ";
        Solution s;
        s.solve();
        i++;
    }

    return 0;
}

void dispvec(vi arr)
{
    vi::iterator itr = arr.begin();
    while (itr != arr.end())
    {
        cout << *itr << " ";
        itr++;
    }
    cout << endl;
}

struct CustomCompare
{
    //stable sort by first then second both in increasing
    bool operator()(const pii &i, const pii &j)
    {
        if (i.first == j.first)
            return i.second < j.second;
        return i.first < j.first;
    }
};

Measured memory_bytes: 11056