#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
map<int, int> mp;
int n, m, mx, gain[N];
vector<pair<int, pair<int, int>>> edges;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    edges.push_back({w, {u, v}});
  }
  sort(edges.rbegin(), edges.rend());
  for (int i = 0; i < edges.size(); ++i) {
    int u = edges[i].second.first, v = edges[i].second.second;
    mp[u] = max(mp[u], gain[v] + 1);
    if (i == m - 1 || edges[i].first > edges[i + 1].first) {
      for (auto &it : mp)
        gain[it.first] = max(gain[it.first], it.second),
        mx = max(mx, gain[it.first]);
      mp.clear();
    }
  }
  cout << mx;
  return 0;
}
