#include<bits/stdc++.h>
using namespace std;

int debug = 1;
int test = 1;

#define printa(a) {for(auto i:a){cout<<i<<" ";} cout<<endl;}
// print debug -> printd()   ||  print for ans -> printa()
#define printd(a) if(debug){cout<<"printing-> ";  for(auto i:a){cout<<setw(2)<<i<<" ";} cout<<endl;}
#define run if(debug)cout<<__LINE__<<endl;
#define Clear( a, b ) memset( a, b, sizeof( a ) )

#define swapp(v) swap(v[0],v[v.size()-1])

#define vvi vector<vector<ll int> >
#define vi vector<int>
#define ff first
#define ss second
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define lb lower_bound
#define ub upper_bound

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)

#define in insert
#define mp make_pair
#define pb push_back
#define pii  pair<ll int,ll int>
#define piii pair<ll int,pair<ll int,ll int> >
#define piiii pair<pair<ll int,ll int>,pair<ll int,ll int> >

#define ll long long

long double pi = 3.14159265358979323846;

#define binf 10000000000050000
#define llinf 1000000000005000000
#define inf 1000050000
#define MOD 1000000007
#define abs llabs
#define BOOST ios::sync_with_stdio(false);  cin.tie(NULL); cout.tie(NULL);

inline ll int min2(ll a, ll b)
{
    if (a < b) {return a;}
    return b;
}
inline long long  max3(long long  a, long long  b, long long  c) {return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));}
inline long long  min3(long long  a, long long b, long long c) {return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));}
//########################################################//
// for modinverse calculation  // here MOD = 10^9+7
ll extgcd(ll a, ll b, ll &x, ll &y) {                     // ExtGCD
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

inline ll mod(ll a, ll m) {                               // mod a wrt b
    return (a % m + m) % m;
}

ll modinv(ll a) {                                         // mod inverse a wrt 10^9+7
    ll x, y;
    extgcd(a, MOD, x, y);
    return mod(x, MOD);
}
//#########################################################//
// GCD
ll gcd(ll u, ll v) {
    ll r;
    while (0 != v) {
        r = u % v; u = v; v = r;
    }
    return u;
}
//###########################################################//
// LCM
ll lcm(ll u, ll v) {
    return u / gcd(u, v) * v;
}
//##########################################################//
//void floyd_warshall(ll n){
//  rep(i,n)rep(j,n)rep(k,n)d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
//}
//##########################################################//
// Get all factors  // cnt in 'a' array
void get_all_factors(int a[], ll int n) {
    ll int w = sqrt(n);
    for (int i = 1; i <= w; i++) {
        if (n % i == 0) {  a[i]++;   a[n / i]++;  }
    }
    ll int x = ceil(sqrt(n)); if (sqrt(n) == ceil(sqrt(n)))  a[(int)sqrt(n)]--;
}
//#################################//
// COMPUTE nCr
ll int memo[100000][110] = {0};
ll C(ll n, ll r)
{
    if (r == 0 || r == n)
        return 1;
    if (memo[n][r] != -1) return memo[n][r];
    return memo[n][r] = ( C(n - 1, r - 1) % MOD + C(n - 1, r) % MOD ) % MOD;
}
//##########################################################//
// Debugging
int valid(string s, int i) { if (s == "lol" or i == -inf) {   return 0;  }  return 1; }
void status(string s1 = "lol", ll int i1 = -inf, string s2 = "lol", ll int i2 = -inf, string s3 = "lol", ll int i3 = -inf) {
    if (debug) {
        if (valid(s1, i1)) {  cout << s1 << "=" << i1 << "  ";  }
        if (valid(s2, i2)) {  cout << s2 << "=" << i2 << "  ";  }
        if (valid(s3, i3)) {  cout << s3 << "=" << i3 << "  ";  }
        cout << endl;
    }
}

//###########################################//
// Compute power with or without MOD

ll int poww(ll x, ll y) {ll res = 1; while (y) {if (y & 1)res = res * x; y = y >> 1; x = x * x;} return res;}
ll int powm(ll x, ll y, ll m = MOD) {x = x % m; ll res = 1; while (y) {if (y & 1)res = res * x; res %= m; y = y >> 1; x = x * x; x %= m;} return res % m;}
//###########################################//
// Matrix Exponentiation
// vector<vector<ll int> > multiply(vvi &a, vvi &b) {
//     vvi c(a.size(), vi (b[0].size()));
//     assert(a[0].size() == b.size());
//     ll int mod = 1000000007;
//     rep(i, a.size()) {
//         rep(j, b[0].size()) {
//             c[i][j] = 0;
//             rep(k, a[0].size()) {
//                 c[i][j] += (a[i][k] * b[k][j]) % mod;
//                 c[i][j] %= mod;
//             }
//         }
//     }
//     return c;
// }
// vector<vector<ll int> > fastmatrix(vector<vector<ll int> >&a, ll int n) {
//     if (n == 1) {  return a; }

//     if (n == 0) {
//         vvi k = a;
//         for (int i = 0; i < a.size(); i++) {
//             for (int j = 0; j < a[i].size(); j++) {
//                 k[i][j] = 1;
//             }
//         }
//         return k;
//     }
//     if (n % 2 == 1) {
//         vvi k = fastmatrix(a, n / 2);  vvi kk = multiply(k, k);  kk = multiply(kk, a);
//         return kk;
//     }
//     else {
//         vvi k = fastmatrix(a, n / 2);  vvi kk = multiply(k, k);
//         return kk;
//     }
// }
// ###########################################//
// Prime Sieve
vector<ll int>prime(10000000, 1);
void sieve() {
    for (int i = 2; i <= 10000; i++) {
        if (prime[i]) {
            for (int j = i * i; j < 10000000; j += i) {
                prime[j] = 0;
            }
        }
    }
}
// ###########################################//

// Dont use these variables------> mod,lcm,gcd,MOD,forn,mp,pb,modinv

// Usable shortcuts
// ff->first , ss->second , abs->>llabs , MOD , forn(j,n) , mp , pb , pii ,
// inf->10^9+5^5 ,

// functns:-->fast(a,n) a^n , gcd(u,v) , lcm(u,v) , mod(a,m) wrt m, modinv(a) wrt MOD, get_all_factors(a[],n) , Clear (a[],b) set all ai to val b,
//            ---------       --------   --------   -------         ---------          ---------------------    -------------
//###########################################//

class comp {  // priority queue custom comparator
public:
    bool operator()(piii &a, piii &b) // write the inverse of what u want to see
    {
        if (a.ff == b.ff)
        {
            return a.ss.ff > b.ss.ff;
        }
        return a.ff < b.ff;

    }

};

//-----------------------------------------//
#define test() int tt=1; if(test){cin>>tt;}  while(tt--)

//###########################################//
ll int getsum(ll int x)
{
    return (x * (x + 1)) / 2;
}
//###########################################//
#define check if(debug){ time_t end=time(NULL); if((end-start)>2500) { cout<<"TLE"<<endl; return 0; }  }
//###########################################//
#define inp(a,n)  { for(int i=0;i<n;i++){cin>>a[i]} }
//###########################################//
#define trace1(x)                cout<<#x<<": "<<x<<endl
#define trace2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
//###########################################//
void filereader() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif
}
//###########################################//

map<int, int>vis;
vvi g(100051);
int dp[200000] = {0};
int solve(int s)
{

    if (dp[s] != 0)
    {
        return dp[s];
    }

    int depth = 0;

    for (auto i : g[s])
    {
        if (!vis[i])
        {
            vis[i] = 1;
            solve(i);
        }
        depth = max(depth, dp[i]);
    }

    return dp[s] = depth + 1;
}

int main() {

    filereader();

    debug = 0;
    test = 0;

    test() {

        // set<int>s;
        // vi v;

        // rep(i, 10)
        // {
        //     s.insert(i * 10);
        //     v.pb(i * 10);
        // }
        // cout << *(s.upper_bound(45)) << endl;
        // cout << *(s.upper_bound(50)) << endl;

        // cout << *(upper_bound(v.begin(), v.end(), 50) ) << endl;

        int n, m;
        cin >> n >> m;

        rep(i, m)
        {
            int x, y;
            cin >> x >> y;

            g[x].pb(y);
        }

        int ans = 0;

        REP(i, n)
        {
            ans = max(ans, solve(i));
        }

        cout << ans - 1 << endl;

    }
}
/*
u

*/

/*

 E sum of digits qn

        int n, k;
        cin >> n >> k;

        k++;

        ll int b = (n - (k * (k + 1)) / 2);

        ll int x = b / (9 * k);

        cout << "val=" << 9 * x*k << endl;
        cout << x << endl;
        cout << endl;
*/

Measured memory_bytes: 96556