#include <bits/stdc++.h>
using namespace std;
struct node {
  int u, v, w;
} arr[300010];
bool cmp(node a, node b) { return (a.w < b.w); }
int n, m;
int f[300010], f2[300010];
int main() {
  scanf("%d", &n);
  ;
  scanf("%d", &m);
  ;
  for (int i = 0; i < m; i++) {
    scanf("%d", &arr[i].u);
    ;
    scanf("%d", &arr[i].v);
    ;
    scanf("%d", &arr[i].w);
    ;
  }
  sort(arr, arr + m, cmp);
  int ans = 0;
  for (int i = 0, j = 0; i < m; i = j) {
    while (j < m && arr[i].w == arr[j].w) j++;
    for (int k = i; k < j; k++) f2[arr[k].v] = f[arr[k].v];
    for (int k = i; k < j; k++)
      f2[arr[k].v] = max(f2[arr[k].v], 1 + f[arr[k].u]);
    for (int k = i; k < j; k++) f[arr[k].v] = f2[arr[k].v];
    for (int k = i; k < j; k++) ans = max(ans, f[arr[k].v]);
  }
  cout << ans;
}
