#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
map<int, int> c[N];
int lowbit(int x) { return x & (-x); }
void update(int x, int y, int val) {
  for (int i = y; i < N; i += lowbit(i)) {
    c[x][i] = max(c[x][i], val);
  }
}
int query(int x, int y) {
  int res = 0;
  for (int i = y; i > 0; i -= lowbit(i)) {
    res = max(res, c[x][i]);
  }
  return res;
}
int main() {
  int n, m, u, v, w;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    w++;
    update(v, w, query(u, w - 1) + 1);
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res = max(res, query(i, N - 4));
  }
  printf("%d\n", res);
}
