#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, pair<int, int>>> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i].second.first >> a[i].second.second >> a[i].first;
  }
  sort(a.begin(), a.end());
  vector<int> len(n + 1);
  for (int i = 0; i < m; i++) {
    vector<pair<int, int>> tmp;
    int j = i;
    while (j < m && a[j].first == a[i].first) {
      tmp.push_back({a[j].second.second, len[a[j].second.first] + 1});
      j++;
    }
    for (int k = 0; k < tmp.size(); k++) {
      len[tmp[k].first] = max(len[tmp[k].first], tmp[k].second);
    }
    i = j - 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, len[i]);
  }
  cout << ans << '\n';
  return 0;
}
