#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
unordered_map<int, int> fen[N];
int get(int a, int i) {
  int rt = 0;
  for (; i; i -= i & (-i)) rt = max(rt, fen[a][i]);
  return (rt);
}
void Do(int a, int i, int x) {
  for (; i < N; i += i & (-i)) fen[a][i] = max(fen[a][i], x);
}
int n, m, ans, a, b, w, tmp;
int main() {
  for (int i = 0; i < N; i++) fen[i].max_load_factor(0.15);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &w);
    ans = max(ans, tmp = get(a, w + 1) + 1);
    Do(b, w + 1, tmp);
  }
  printf("%d", ans);
}
