#include <bits/stdc++.h>
using namespace std;
struct node {
  int u, v, w;
} a[350000];
int cmp(node a, node b) { return a.w < b.w; }
int pre[3500000];
int dp[350000];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    memset(pre, 0, sizeof(pre));
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < m; i++) scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
    sort(a, a + m, cmp);
    for (int i = 0; i < m; i++) {
      queue<int> s;
      int end = i;
      while (end < m && a[end].w == a[end + 1].w) end++;
      for (int j = i; j <= end; j++) {
        int tmp = dp[a[j].v];
        dp[a[j].v] = max(pre[a[j].u] + 1, dp[a[j].v]);
        if (tmp != dp[a[j].v]) s.push(a[j].v);
      }
      while (!s.empty()) {
        int u = s.front();
        s.pop();
        pre[u] = max(pre[u], dp[u]);
      }
      i = end;
    }
    int output = 0;
    for (int i = 1; i <= n; i++) output = max(output, pre[i]);
    printf("%d\n", output);
  }
}
