#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  set<pair<long long, long long> > a[n + 5];
  for (long long i = 1; i <= n; i++) a[i].insert({-1, 0});
  while (m--) {
    long long u, v, w;
    cin >> u >> v >> w;
    auto it = a[u].lower_bound({w, -1});
    it--;
    long long d = it->second + 1;
    it = a[v].lower_bound({w + 1, -1});
    it--;
    if (it->second >= d) continue;
    while (true) {
      it = a[v].lower_bound({w, -1});
      if (it == a[v].end()) break;
      if (it->second > d) break;
      a[v].erase(it);
    }
    a[v].insert({w, d});
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    auto it = a[i].end();
    it--;
    ans = max(ans, it->second);
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long T = 1;
  while (T--) solve();
}
