#include <bits/stdc++.h>
using namespace std;
inline int v_in() {
  int f = 1, sum = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    sum = (sum << 3) + (sum << 1) + (ch ^ 48);
    ch = getchar();
  }
  return sum * f;
}
const int M = 5e5 + 5;
int n, m, g, t, ans, fst[M], f[M], h[M];
struct nd {
  int u, v, w;
} p[M << 1];
inline bool cmp(const nd a, const nd b) { return a.w < b.w; }
int main() {
  n = v_in(), m = v_in();
  for (register int i = 1; i <= m; i = -~i) p[i] = (nd){v_in(), v_in(), v_in()};
  sort(p + 1, p + m + 1, cmp);
  for (register int i = 1; i <= m; i = -~i) {
    f[i] = h[p[i].u] + 1;
    if (p[i].w != p[i + 1].w) {
      for (register int j = t; j <= i; j = -~j)
        h[p[j].v] = max(h[p[j].v], f[j]);
      t = i + 1;
    }
    ans = max(ans, f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
