#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, w, dp[300007], t[300007], res;
vector<pair<int, int>> ee[300050];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  std::cin >> n >> m;
  for (int i = 0; i < m; i++) std::cin >> u >> v >> w, ee[w].push_back({u, v});
  for (int i = 0; i < 300001; i++) {
    for (auto e : ee[i]) t[e.second] = 0;
    for (auto e : ee[i]) t[e.second] = max(t[e.second], dp[e.first] + 1);
    for (auto e : ee[i]) {
      dp[e.second] = max(t[e.second], dp[e.second]);
      res = max(res, dp[e.second]);
    }
  }
  std::cout << res << '\n';
}
