#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
long long INF = 1000000000000000005;
long long add(long long a, long long b) {
  return ((a % MOD) + (b % MOD)) % MOD;
}
long long mul(long long a, long long b) {
  return ((a % MOD) * (b % MOD)) % MOD;
}
long long sub(long long a, long long b) {
  return ((a % MOD) - (b % MOD) + MOD) % MOD;
}
long long n, m, i, dp[300003], u, v, w, mx, len, temp[300003], j;
vector<pair<long long, long long> > edges[300003];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v >> w;
    edges[w].push_back({u, v});
  }
  for (i = 0; i < 300003; i++) {
    len = edges[i].size();
    for (j = 0; j < len; j++) {
      u = edges[i][j].first;
      v = edges[i][j].second;
      temp[v] = 0;
    }
    for (j = 0; j < len; j++) {
      u = edges[i][j].first;
      v = edges[i][j].second;
      temp[v] = max(temp[v], dp[u] + 1);
    }
    for (j = 0; j < len; j++) {
      u = edges[i][j].first;
      v = edges[i][j].second;
      dp[v] = max(dp[v], temp[v]);
      mx = max(mx, dp[v]);
    }
  }
  cout << mx;
  return 0;
}
