#include <bits/stdc++.h>
using namespace std;
int n, m;
map<int, int> Q[100005];
map<int, int>::iterator it;
int lowbit(int x) { return x & -x; }
void add(int v, int w, int x) {
  while (w < 100005) {
    Q[v][w] = max(Q[v][w], x);
    w += lowbit(w);
  }
}
int sum(int v, int w) {
  int ret = 0;
  while (w) {
    it = Q[v].find(w);
    if (it != Q[v].end()) ret = max(ret, it->second);
    w -= lowbit(w);
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    w++;
    add(v, w, sum(u, w - 1) + 1);
  }
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    ret = max(ret, sum(i, 100005));
  }
  cout << ret << endl;
  return 0;
}
