#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long n, m;
long long dp[300005], tmp[300005];
vector<pair<long long, long long> > v[300005];
void solve() {
  cin >> n >> m;
  for (long long i = 0, w, a, b; i < m; i++) {
    cin >> a >> b >> w;
    v[w].push_back({a, b});
  }
  for (long long i = 300005; i >= 0; i--) {
    for (pair<long long, long long> it : v[i]) {
      tmp[it.first] = 0;
    }
    for (pair<long long, long long> it : v[i]) {
      tmp[it.first] = max(tmp[it.first], dp[it.second] + 1);
    }
    for (pair<long long, long long> it : v[i]) {
      dp[it.first] = max(dp[it.first], tmp[it.first]);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}
