#include <bits/stdc++.h>
using namespace std;
int d[(2 * 100000 + 5) * 55], ls[(2 * 100000 + 5) * 55],
    rs[(2 * 100000 + 5) * 55], rt[5 * (2 * 100000 + 5)], x[(2 * 100000 + 5)],
    y[(2 * 100000 + 5)], dd[(2 * 100000 + 5)], n, m, cnt, sjn;
void insert(int &k, int l, int r, int x, int y) {
  if (!k) k = ++cnt;
  d[k] = max(d[k], y);
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (x <= mid)
    insert(ls[k], l, mid, x, y);
  else
    insert(rs[k], mid + 1, r, x, y);
}
int query(int k, int l, int r, int x, int y) {
  if (!k || x > y || l > r) return 0;
  if (x <= l && y >= r) return d[k];
  int mid = (l + r) >> 1;
  if (y <= mid)
    return query(ls[k], l, mid, x, y);
  else if (x > mid)
    return query(rs[k], mid + 1, r, x, y);
  else
    return max(query(ls[k], l, mid, x, mid),
               query(rs[k], mid + 1, r, mid + 1, y));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &x[i], &y[i], &dd[i]);
  for (int i = 1; i <= m; i++) {
    int tmp = query(rt[x[i]], 0, 100000, 0, dd[i] - 1);
    sjn = max(sjn, tmp + 1);
    insert(rt[y[i]], 0, 100000, dd[i], tmp + 1);
  }
  printf("%d\n", sjn);
  return 0;
}
