#include <bits/stdc++.h>
using namespace std;
int INF = 1e9 + 7;
long long INFLL = 1e17;
const int maxn = 2e5 + 5;
int n, m;
map<int, int> dp[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i < (n + 1); ++i) dp[i][-1] = 0;
  int ans = 0;
  for (int i = 0; i < (m); ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    auto val = prev(dp[u].upper_bound(w))->second + 1;
    ans = max(ans, val);
    val = dp[v][w] = max(dp[v][w], val);
    auto lo = dp[v].find(w);
    lo--;
    if (lo->second >= val) {
      dp[v].erase(w);
    } else {
      auto it = dp[v].upper_bound(w);
      while (it != dp[v].end() && val >= it->second) {
        it++;
        dp[v].erase(prev(it));
      }
    }
  }
  cout << ans << endl;
}
