#include <bits/stdc++.h>
using namespace std;
const int N = 300014;
struct e {
  int u, v, w, id;
} ed[N];
int n, m, F[N], G[N];
bool cmp(e a, e b) { return a.w < b.w; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &ed[i].u, &ed[i].v, &ed[i].w);
  sort(ed + 1, ed + m + 1, cmp);
  int k = 1, ans = 0;
  ed[0].w = ed[1].w;
  for (int i = 1; i <= m; i++) {
    int u, v;
    if (ed[i].w != ed[i - 1].w) {
      for (int j = k; j < i; j++) G[ed[j].v] = F[ed[j].v];
      k = i;
    }
    u = ed[i].u, v = ed[i].v;
    F[v] = max(F[v], G[u] + 1);
    ans = max(ans, F[v]);
  }
  printf("%d", ans);
}
