#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FOR(i,a,b) for (int i=(a),for_##i##_max=(b);i<=(for_##i##_max);++i)
#define RFOR(i,a,b) for (int i=(a),rfor_##i##_min=(b);i>=(rfor_##i##_min);--i)
#define REP(i,n) for (int i=0,rep_##i##_len=(n);i<(rep_##i##_len);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

namespace { // Basics

template<typename T>
void chmin(T &x, T y) {
  x = std::min(x, y);
}

template<typename T>
void chmax(T &x, T y) {
  x = std::max(x, y);
}

template<typename T>
__attribute__((unused))
const auto minimum = static_cast<T const &(*)(T const &, T const &)>(std::min);

template<typename T>
__attribute__((unused))
const auto maximum = static_cast<T const &(*)(T const &, T const &)>(std::max);

template<typename T>
int cmp3(T lhs, T rhs) { return (lhs == rhs) ? 0 : (lhs > rhs) ? 1 : -1; }

template<typename T>
__attribute__((unused))
T constexpr infty = std::numeric_limits<T>::max / 3;
template<>
__attribute__((unused))
auto constexpr infty<int> = 1'100'100'100;
template<>
__attribute__((unused))
auto constexpr infty<long long int> = 100'500'400'300'200'100LL;

using lli = long long int;
using ld = long double;

} // namespace
namespace { // Graph Basics

template<typename WeightT>
struct edge {
  int from, to;
  WeightT weight;
  edge(int s, int d, WeightT w): from(s), to(d), weight(w) {}
};

template<typename WeightT>
bool
operator< (edge<WeightT> const &lhs, edge<WeightT> const &rhs) {
  if (lhs.weight != rhs.weight) {
    return lhs.weight < rhs.weight;
  } else if (lhs.from != rhs.from) {
    return lhs.from < rhs.from;
  }
  return lhs.to < rhs.to;
}

template<typename WeightT>
using edges = std::vector<edge<WeightT> >;

template<typename WeightT>
using adjacency_list = std::vector<edges<WeightT> >;

template<typename WeightT>
void
add_undir_edge(adjacency_list<WeightT> &G, int u, int v, WeightT w) {
  G[u].emplace_back(u, v, w);
  G[v].emplace_back(v, u, w);
}

template<typename WeightT>
adjacency_list<WeightT>
inverse_graph(adjacency_list<WeightT> const &G) {
  size_t const n = G.size();
  adjacency_list<WeightT> F(n);
  for (auto &&es: G) for (auto &&e: es) {
    F[e.to].emplace_back(e.to, e.from, e.weight);
  }
  return F;
}

template <typename WeightT>
edges<WeightT>
all_edges(adjacency_list<WeightT> const &G) {
  auto const E = std::accumulate(begin(G), end(G), 0,
      [](auto x, auto vec) { return x + vec.size(); });
  edges<WeightT> es;
  es.reserve(E);
  for (auto &&vec: G) {
    std::copy(begin(vec), end(vec), std::back_inserter(es));
  }
  return es;
}

} // namespace

template<typename WeightT>
std::vector<size_t> kahn_tsort(adjacency_list<WeightT> const &G) {
  std::vector<size_t> ideg(G.size(), 0);
  for (size_t u = 0; u < G.size(); ++u) for (auto &&e : G[u]) { ++ideg[e.to]; }

  std::stack<size_t> stk;
  for (size_t u = 0; u < G.size(); ++u) {
    if (ideg[u] == 0) { stk.push(u); }
  }

  std::vector<size_t> vec;
  while (!stk.empty()) {
    size_t r = stk.top(); stk.pop(); vec.push_back(r);
    for (auto &&e : G[r]) {
      --ideg[e.to];
      if (ideg[e.to] == 0) { stk.push(e.to); }
    }
  }
  return vec;
}

using namespace std;

using Graph = adjacency_list<nullptr_t>;

void solve(Graph const &G) {
  auto const dep = kahn_tsort(G);
  auto const N = G.size();
  vector<size_t> idx(N);
  REP(i, N) { idx[dep[i]] = i; }
  vector<size_t> dp(N, 0);
  REP(i, N-1) {
    auto u = dep[i];
    for (auto &&e : G[u]) {
      chmax(dp[idx[e.to]], dp[i] + 1);
    }
  }
  cout << accumulate(begin(dp), end(dp), 0, maximum<size_t>) << endl;
}

int main() {
  // cout << fixed << setprecision(10);

  int N, M; cin >> N >> M;
  Graph G(N);
  REP(i, M) {
    int X, Y;
    cin >> X >> Y; --X, --Y;
    G[X].emplace_back(X, Y, nullptr);
  }
  solve(G);
  return 0;
}


Measured memory_bytes: 8036