#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 2;
const int maxx = 1e13;
const int mod1 = 1000000007;
const int mod = 998244353;
const int base = 31;
template <class T>
void read(T& x) {
  register int c;
  T neg = 1;
  x = (T)0;
  while ((c = getchar()) <= 47 || c >= 58)
    if (c == '-') neg = -1;
  for (; (c > 47 && c < 58); c = getchar()) {
    x = (x << 3) + (x << 1) + (T)(c - 48);
  }
  x *= neg;
}
struct tk {
  int u;
  int v;
  int w;
};
bool lf(tk& a, tk& b) { return a.w <= b.w; }
int dp[maxn];
int n, m;
tk a[maxn];
vector<pair<int, int> > vt;
int mx = 0;
vector<pair<int, int> > wt[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) {
    read(a[i].u);
    read(a[i].v);
    read(a[i].w);
    wt[a[i].w].push_back(make_pair(a[i].u, a[i].v));
  }
  for (int i = 1; i <= n; i++) dp[i] = 0;
  for (int i = 0; i < maxn; i++) {
    vt.clear();
    for (int j = 0; j < wt[i].size(); j++) {
      auto p = wt[i][j];
      vt.push_back(make_pair(p.second, dp[p.first] + 1));
    }
    for (auto p : vt) {
      dp[p.first] = max(dp[p.first], p.second);
    }
  }
  for (int i = 1; i <= n; i++) {
    mx = max(mx, dp[i]);
  }
  cout << mx;
  return 0;
}
