#include <bits/stdc++.h>
const int N = 1e5 + 5;
int n, m;
struct Chairman {
  static const int M = 20 * N;
  int idx, rt[N], ls[M], rs[M], val[M];
  void modify(int &p, int l, int r, int u, int x, int v) {
    p = ++idx, ls[p] = ls[u], rs[p] = rs[u], val[p] = std::max(val[u], v);
    if (l == r) {
      return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) {
      modify(ls[p], l, mid, ls[u], x, v);
    } else {
      modify(rs[p], mid + 1, r, rs[u], x, v);
    }
  }
  int query(int p, int l, int r, int x, int y) {
    if (x <= l && r <= y) {
      return val[p];
    }
    int mid = (l + r) >> 1;
    if (y <= mid) {
      return query(ls[p], l, mid, x, y);
    } else if (x > mid) {
      return query(rs[p], mid + 1, r, x, y);
    } else {
      return std::max(query(ls[p], l, mid, x, mid),
                      query(rs[p], mid + 1, r, mid + 1, y));
    }
  }
} cha;
int main() {
  scanf("%d%d", &n, &m);
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w), w += 2;
    int now = cha.query(cha.rt[u], 1, 100001, 1, w - 1) + 1;
    cha.modify(cha.rt[v], 1, 100001, cha.rt[v], w, now);
    ans = std::max(ans, now);
  }
  printf("%d\n", ans);
  return 0;
}
