#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int> > > adj;
int dp[3 * 100000 + 7];
int tmp[3 * 100000 + 7];
int main(int argc, const char* argv[]) {
  int n, m, x, y, w;
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> w;
    adj.push_back(pair<int, pair<int, int> >(w, pair<int, int>(x, y)));
  }
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0;
    tmp[i] = 0;
  }
  sort(adj.begin(), adj.end());
  int i = 0;
  while (i < adj.size()) {
    int j = i;
    while (adj[i].first == adj[j + 1].first) j++;
    for (int k = i; k <= j; k++) {
      tmp[adj[k].second.second] =
          max(tmp[adj[k].second.second], dp[adj[k].second.first] + 1);
    }
    for (int k = i; k <= j; k++) {
      dp[adj[k].second.second] =
          max(tmp[adj[k].second.second], dp[adj[k].second.second]);
    }
    i = j + 1;
  }
  int ans = -1;
  for (int i = 1; i < n + 1; i++) ans = ((ans) > (dp[i]) ? ans : dp[i]);
  cout << ans << endl;
  return 0;
}
