#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int N = 100000;
set<pair<int, int>> dp[N + 1];
void add_edge(int a, int b, int weight) {
  auto it = prev(dp[a].lower_bound({weight, -INF}));
  int X = (*it).second;
  pair<int, int> val = {weight, X + 1};
  if (prev(dp[b].lower_bound(val))->second > X + 1) return;
  while (1) {
    auto f = dp[b].upper_bound(val);
    if (f == dp[b].end()) break;
    if (f->second <= val.second)
      dp[b].erase(f);
    else
      break;
  }
  dp[b].insert(val);
}
void solve() {
  int n, m;
  cin >> n >> m;
  int u, v, wt;
  for (int i = 1; i <= n; i++) {
    dp[i].insert({-1, 0});
  }
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> wt;
    add_edge(u, v, wt);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, (dp[i].rbegin())->second);
  }
  cout << ans << endl;
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  for (int tc = 1; tc <= tt; tc++) {
    solve();
  }
}
