#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
map<int, int> d[100000];
inline int getCnt(int v, int w) {
  if (d[v].empty()) {
    return 0;
  }
  auto it = d[v].lower_bound(w);
  if (it == d[v].begin()) {
    return 0;
  }
  return (--it)->second;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int ans = 0;
  for (int i = 0; i < m; ++i) {
    int from, to, w;
    cin >> from >> to >> w;
    --from;
    --to;
    int cnt = getCnt(from, w) + 1;
    if (cnt > getCnt(to, w + 1)) {
      d[to][w] = cnt;
      for (auto it = d[to].upper_bound(w);
           it != d[to].end() && cnt > it->second;) {
        it = d[to].erase(it);
      }
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\n";
  return 0;
}
