#include <bits/stdc++.h>
using namespace std;
const int MAXE = 300005;
const int MAXN = 300005;
int u[MAXE];
int v[MAXE];
int w[MAXE];
int p[MAXE];
int best[MAXN];
int prevBest[MAXN];
int lastW[MAXN];
bool cmp(const int a, const int b) { return w[a] < w[b]; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", u + i, v + i, w + i);
    p[i] = i;
  }
  sort(p, p + m, cmp);
  for (int i = 0; i < m; i++) {
    int from = u[p[i]];
    int to = v[p[i]];
    int weight = w[p[i]];
    int ff = (lastW[from] == weight) ? prevBest[from] : best[from];
    if (ff + 1 > best[to]) {
      prevBest[to] = best[to];
      best[to] = ff + 1;
      lastW[to] = weight;
    }
  }
  printf("%d\n", *max_element(best + 1, best + n + 1));
  return 0;
}
