#include <bits/stdc++.h>
using namespace std;
int dp1[300001], dp[300001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  map<int, std::vector<std::pair<int, int> > > w;
  map<int, std::vector<std::pair<int, int> > >::iterator it;
  int u, v, wi;
  for (long long int i = 0; i <= m - 1; i++) {
    cin >> u >> v >> wi;
    w[wi].push_back(make_pair(u, v));
  }
  for (it = w.begin(); it != w.end(); it++) {
    std::vector<std::pair<int, int> > x = it->second;
    std::vector<std::pair<int, int> >::iterator it2;
    for (it2 = x.begin(); it2 != x.end(); it2++) {
      u = it2->first;
      v = it2->second;
      dp1[v] = max(dp1[v], dp[u] + 1);
    }
    for (it2 = x.begin(); it2 != x.end(); it2++) {
      u = it2->first;
      v = it2->second;
      dp[v] = max(dp1[v], dp[v]);
    }
  }
  int ans = -1;
  for (long long int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << "\n";
}
