#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
constexpr int N = 1e5 + 5;
int n, m, a, b, c, dp[3 * N], ans = 0, temp[3 * N];
vector<pii> edges[N];
void Solve() {
  cin >> n >> m;
  while (m--) {
    cin >> a >> b >> c;
    edges[c].push_back({a, b});
  }
  for (int i = 1; i < N; i++) {
    for (auto [u, v] : edges[i]) temp[v] = 0;
    for (auto [u, v] : edges[i]) temp[v] = max(temp[v], dp[u] + 1);
    for (auto [u, v] : edges[i]) dp[v] = max(temp[v], dp[v]);
  }
  for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}
