#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int inf = (1 << 30) - 1;
const int sz = (int)3e5 + 5;
int n, m, v, ans, u, c;
set<pair<int, int> > s[sz];
void add(pair<int, int> r, int v) {
  s[v].insert(r);
  auto it = s[v].lower_bound(r);
  if (it != s[v].begin()) {
    it--;
    if ((*it).second >= r.second) {
      s[v].erase(r);
      return;
    }
    if ((*it).first == r.first) s[v].erase(it);
  }
  it = s[v].lower_bound(r);
  vector<pair<int, int> > to_erase;
  it++;
  while (it != s[v].end()) {
    if ((*it).second <= r.second) {
      to_erase.push_back(*it);
    }
    it++;
  }
  for (auto x : to_erase) {
    s[v].erase(x);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> v >> u >> c;
    int cur = 0;
    auto it = s[v].lower_bound(make_pair(c, 0));
    if (!s[v].empty() && it != s[v].begin()) {
      it--;
      if (it != s[v].end()) {
        cur = (*it).second;
      }
    }
    add(make_pair(c, cur + 1), u);
    ans = max(ans, cur + 1);
  }
  cout << ans;
}
