#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
const int maxw = 1000000;
const int oo = (int)1e9;
int n, m;
int x[maxn + 1], y[maxn + 1], w[maxn + 1];
map<int, int> best[maxn + 1];
map<int, int> RR;
int res;
int getBIT(int id, int x) {
  int res = 0;
  for (; x > 0; x &= x - 1) {
    if (best[id].count(x)) res = max(res, best[id][x]);
  }
  return res;
}
void updateBIT(int id, int x, int v) {
  for (; x <= RR.size(); x += x & (-x)) {
    if (best[id].count(x))
      best[id][x] = max(best[id][x], v);
    else
      best[id][x] = v;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) best[i].clear();
  res = 0;
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i] >> w[i];
    RR[w[i]] = 0;
  }
  int dem = 0;
  for (auto it = RR.begin(); it != RR.end(); it++) {
    dem++;
    (*it).second = dem;
  }
  for (int i = 1; i <= m; i++) w[i] = RR[w[i]];
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i] >> w[i];
    int p = getBIT(x[i], w[i]);
    updateBIT(y[i], w[i], p + 1);
    res = max(res, p + 1);
  }
  cout << res;
  return 0;
}
