#include <bits/stdc++.h>
using namespace std;
set<pair<long long, long long> > s[100000];
long long ans = 0;
void f(long long a, long long b, long long w) {
  auto it = prev(s[a].lower_bound({w, -2}));
  pair<long long, long long> x = {w, it->second + 1};
  it = prev(s[b].lower_bound({x.first + 1, -1}));
  if (it->second >= x.second) return;
  while (1) {
    it = s[b].lower_bound({x.first, -1});
    if (it == s[b].end()) break;
    if (it->second > x.second) break;
    s[b].erase(it);
  }
  s[b].insert(x);
}
int main() {
  long long n, m, i, a, b, w;
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < n; i++) s[i].insert({-1, 0});
  for (i = 0; i < m; i++)
    scanf("%lld%lld%lld", &a, &b, &w), a--, b--, f(a, b, w);
  ans = 0;
  for (i = 0; i < n; i++) {
    auto it = prev(s[i].end());
    ans = ((ans > it->second) ? ans : it->second);
  }
  printf("%lld\n", ans);
}
