#include <bits/stdc++.h>
using namespace std;
int main() {
  long long(n), (m);
  scanf("%lld%lld", &(n), &(m));
  ;
  vector<map<long long, long long>> vals(n);
  long long maxi = 1;
  for (long long i = 0; i < (m); i++) {
    long long(a), (b), (c);
    scanf("%lld%lld%lld", &(a), &(b), &(c));
    ;
    --a;
    --b;
    auto it = vals[a].lower_bound(c);
    long long l;
    if (!vals[a].size() || it == vals[a].begin())
      l = 1;
    else {
      it--;
      l = (*it).second + 1;
    }
    maxi = max(maxi, l);
    auto it2 = vals[b].upper_bound(c);
    if (!vals[b].size())
      vals[b][c] = l;
    else {
      if (it2 != vals[b].begin()) {
        it2--;
        if ((*it2).second >= l) continue;
      }
      vals[b][c] = l;
      it2 = vals[b].lower_bound(c);
      it2++;
      vector<long long> v;
      while (it2 != vals[b].end() && (it2->second) <= l) {
        v.push_back(it2->first);
        it2++;
      }
      for (auto j : v) vals[b].erase(j);
    }
  }
  printf("%lld\n", maxi);
  return 0;
}
