#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char i = getchar();
  long long f = 1, res = 0;
  while (i < '0' || i > '9') {
    if (i == '-') f = -1;
    i = getchar();
  }
  while (i >= '0' && i <= '9') {
    res = res * 10 + i - '0';
    i = getchar();
  }
  return res * f;
}
int n, m, cnt, ans;
int root[500010], val[500010 << 2], ch[500010 << 2][2];
inline void insert(int &rt, int l, int r, int k, int x) {
  if (!rt) rt = ++cnt;
  val[rt] = max(val[rt], x);
  if (l == r) return;
  int mid = (l + r) / 2;
  if (k <= mid)
    insert(ch[rt][0], l, mid, k, x);
  else
    insert(ch[rt][1], mid + 1, r, k, x);
}
inline int query(int rt, int l, int r, int l1, int r1) {
  if (!rt) return 0;
  if (l1 > r || r1 < l) return 0;
  if (l1 <= l && r1 >= r) return val[rt];
  int mid = (l + r) / 2;
  return max(query(ch[rt][0], l, mid, l1, r1),
             query(ch[rt][1], mid + 1, r, l1, r1));
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read(), z = read() + 1;
    int x = query(root[a], 1, 100000, 1, z - 1) + 1;
    ans = max(ans, x);
    insert(root[b], 1, 100001, z, x);
  }
  printf("%d", ans);
  return 0;
}
