#include <bits/stdc++.h>
using namespace std;
struct node {
  int u, v, w;
} e[300005];
int n, m, ans;
int f[300005], g[300005];
bool operator<(node x, node y) { return x.w < y.w; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
  sort(e + 1, e + m + 1);
  int t = 1;
  for (int i = 1; i <= m; i++) {
    f[i] = g[e[i].u] + 1;
    if (e[i].w != e[i + 1].w) {
      for (int j = t; j <= i; j++) g[e[j].v] = max(g[e[j].v], f[j]);
      t = i + 1;
    }
    ans = max(ans, f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
