#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int INF = 1e8;
int N, M;
set<pair<int, int> > dp[MAXN];
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) {
    dp[i].insert({-1, 0});
  }
  int ans = 0;
  for (int i = 1; i <= M; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    pair<int, int> dat(w, prev(dp[u].lower_bound({w, -INF}))->second + 1);
    ans = max(ans, dat.second);
    if (prev(dp[v].upper_bound({w, INF}))->second >= dat.second) {
      continue;
    }
    dp[v].insert(dat);
    while (true) {
      auto it = dp[v].upper_bound(dat);
      if (it != dp[v].end() && it->second <= dat.second) {
        dp[v].erase(it);
      } else {
        break;
      }
    }
  }
  printf("%d\n", ans);
}
