#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 200005;
void go() {
  int n, m;
  cin >> n >> m;
  map<int, int> mp[n + 5];
  for (int i = 1; i < n + 1; i++) {
    mp[i][-1] = 0;
  }
  int mx = 0;
  for (int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    auto ptr = prev(mp[x].lower_bound(w));
    int nxtlen = (*ptr).second + 1;
    auto ptr2 = prev(mp[y].upper_bound(w));
    if ((*ptr2).second >= nxtlen) {
      continue;
    }
    ptr2 = next(ptr2);
    vector<int> er;
    while (ptr2 != mp[y].end()) {
      if ((*ptr2).second <= nxtlen) {
        er.push_back((*ptr2).first);
        ptr2 = next(ptr2);
      } else {
        break;
      }
    }
    mp[y][w] = nxtlen;
    mx = max(mx, nxtlen);
    for (auto &i : er) {
      mp[y].erase(i);
    }
  }
  cout << mx << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int c = 0;
  int t;
  if (!c) {
    t = 1;
  } else {
    cin >> t;
  }
  while (t--) {
    go();
  }
}
