#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
set<pair<int, int> > st[MAX_N];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (int)(n); ++i) {
    st[i].insert(pair<int, int>(-1, 0));
  }
  for (int i = 0; i < (int)(m); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    pair<int, int> p = *(--st[a].lower_bound(pair<int, int>(c, -1000000005)));
    p = pair<int, int>(c, p.second + 1);
    while (1) {
      auto it = st[b].lower_bound(p);
      if (it == st[b].end()) {
        break;
      }
      if (it->second <= p.second) {
        st[b].erase(it);
      } else {
        break;
      }
    }
    auto it = --st[b].lower_bound(p);
    if (it->second < p.second) {
      st[b].insert(p);
    }
  }
  int mx = 0;
  for (int i = 0; i < (int)(n); ++i) {
    for (auto&(it) : (st[i])) {
      mx = max(mx, it.second);
    }
  }
  cout << mx << "\n";
  return 0;
}
