#include <bits/stdc++.h>
namespace remoon {
inline int read() {
  int p = 0, w = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') p = p * 10 + c - '0', c = getchar();
  return p * w;
}
int wr[50], rw;
template <typename ra>
inline void write(ra o, char c = '\n') {
  if (!o) putchar('0');
  if (o < 0) o = -o, putchar('-');
  while (o) wr[++rw] = o % 10, o /= 10;
  while (rw) putchar(wr[rw--] + '0');
  putchar(c);
}
template <typename ra>
inline void cmin(ra &a, ra b) {
  if (a > b) a = b;
}
template <typename ra>
inline void cmax(ra &a, ra b) {
  if (a < b) a = b;
}
template <typename ra>
inline bool ckmin(ra &a, ra b) {
  return (a > b) ? a = b, 1 : 0;
}
template <typename ra>
inline bool ckmax(ra &a, ra b) {
  return (a < b) ? a = b, 1 : 0;
}
}  // namespace remoon
using namespace std;
using namespace remoon;
namespace mod_zone {
inline void inc(int &a, int b) {
  a += b;
  if (a >= 998244353) a -= 998244353;
}
inline void dec(int &a, int b) {
  a -= b;
  if (a < 0) a += 998244353;
}
inline int Inc(int a, int b) {
  return (a + b >= 998244353) ? a + b - 998244353 : a + b;
}
inline int Dec(int a, int b) { return (a - b < 0) ? a - b + 998244353 : a - b; }
inline int mul(int a, int b) { return 1ll * a * b % 998244353; }
inline int fp(int a, int k) {
  int ret = 1;
  for (; k; k >>= 1, a = mul(a, a))
    if (k & 1) ret = mul(ret, a);
  return ret;
}
}  // namespace mod_zone
using namespace mod_zone;
int n, m, id, ans;
int rt[100050], ls[8005000], rs[8005000], mx[8005000];
inline int qry(int &now, int ml, int mr, int l = -2, int r = 100000) {
  if (!now || ml > r || mr < l) return 0;
  if (ml <= l && mr >= r) return mx[now];
  int mid = (l + r) >> 1;
  return max(qry(ls[now], ml, mr, l, mid), qry(rs[now], ml, mr, mid + 1, r));
}
inline void ins(int &now, int p, int v, int l = -2, int r = 100000) {
  if (!now) now = ++id;
  cmax(mx[now], v);
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (p <= mid)
    ins(ls[now], p, v, l, mid);
  else
    ins(rs[now], p, v, mid + 1, r);
}
int main() {
  n = read();
  m = read();
  for (register int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read();
    int tmp = qry(rt[u], -2, w - 1) + 1;
    ins(rt[v], w, tmp);
    cmax(ans, tmp);
  }
  write(ans);
  return 0;
}
