#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, m;
vector<pair<int, int> > g[N];
int dp[N], cur[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    g[c].push_back({u, v});
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (auto el : g[i]) {
      int v = el.second;
      cur[v] = 0;
    }
    for (auto el : g[i]) {
      int u = el.first, v = el.second;
      cur[v] = max(cur[v], dp[u] + 1);
    }
    for (auto el : g[i]) {
      int u = el.first, v = el.second;
      dp[v] = max(dp[v], cur[v]);
    }
  }
  for (int i = 0; i < N; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
