#include <bits/stdc++.h>
using namespace std;
int n, m;
int const N = 3e5 + 3;
int ans[N];
int temp[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  vector<pair<int, pair<int, int>>> edges;
  for (int i = (0); i < (m); ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    edges.push_back({w, {u, v}});
  }
  sort(begin(edges), end(edges));
  fill(ans, ans + n, 0);
  fill(temp, temp + n, 0);
  for (int i = (0); i < (m); ++i) {
    vector<int> cand;
    int cur = edges[i].first;
    while (i < m && edges[i].first == cur) {
      int u = edges[i].second.first;
      int v = edges[i].second.second;
      cand.push_back(v);
      temp[v] = max(temp[v], ans[u] + 1);
      ++i;
    }
    --i;
    for (auto& e : cand) {
      ans[e] = max(temp[e], ans[e]);
      temp[e] = 0;
    }
  }
  cout << *max_element(ans, ans + n) << '\n';
  return 0;
}
