#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vii = vector<int>;
using pii = pair<int, int>;
template <typename T = vii>
using vec = vector<T>;
const ll INF = 1e18, MOD = 1e9 + 7;
const int MAX = 1e6;
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  map<int, vec<pii> > d;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    d[w].emplace_back(--u, --v);
  }
  vii dp(n);
  for (auto [_, V] : d) {
    map<int, int> new_dp;
    for (auto [u, v] : V) {
      new_dp[v] = max({new_dp[v], dp[v], dp[u] + 1});
    }
    for (auto [i, x] : new_dp) {
      dp[i] = x;
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';
}
