#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
vector<pair<int, int>> edges[maxn];
void solve() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<int> dp(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    edges[c].push_back({a, b});
  }
  vector<int> temp(n + 1);
  for (int i = 0; i < maxn; i++) {
    for (auto it : edges[i]) temp[it.second] = 0;
    for (auto it : edges[i]) {
      int from = it.first;
      int to = it.second;
      temp[to] = max(temp[to], dp[from] + 1);
    }
    for (auto it : edges[i]) {
      int from = it.first;
      int to = it.second;
      dp[to] = max(dp[to], temp[to]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) solve();
  return 0;
}
