#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-O2")
const long long LIM = 2e5 + 5;
const long long MOD = 1e9 + 7;
const long long inf = 1e10;
void mmg(vector<vector<int> >& adj, int m, vector<int>& deg) {
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x - 1].push_back(y - 1);
    deg[y - 1]++;
  }
  return;
}
void topo(vector<int>& T, queue<int>& q, vector<vector<int> >& adj,
          vector<int>& deg) {
  int x;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    T.push_back(x);
    for (vector<int>::iterator it = adj[x].begin(); it != adj[x].end(); ++it) {
      deg[*it]--;
      if (deg[*it] == 0) q.push(*it);
    }
  }
  return;
}
void maxpath(vector<int>& T, vector<vector<int> >& adj, vector<int>& dp,
             int n) {
  for (int i = 0; i < n; i++) {
    for (vector<int>::iterator it = adj[(T[i])].begin(); it != adj[T[i]].end();
         ++it) {
      dp[*it] = max(dp[T[i]] + 1, dp[*it]);
    }
  }
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > adj;
  adj.resize(n);
  vector<int> deg;
  for (int i = 0; i < n; i++) deg.push_back(0);
  mmg(adj, m, deg);
  vector<int> T, dp;
  for (int i = 0; i < n; i++) dp.push_back(-1);
  queue<int> q;
  for (int i = 0; i < n; i++)
    if (deg[i] == 0) {
      q.push(i);
      dp[i] = 0;
    }
  topo(T, q, adj, deg);
  maxpath(T, adj, dp, n);
  int maxx = 0;
  for (int i = 0; i < n; i++) maxx = max(dp[i], maxx);
  cout << maxx;
}
