#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, w;
  edge() { u = v = w = 0; }
  void input(void) { scanf("%d%d%d", &u, &v, &w); }
  bool operator<(const edge &x) const { return (w < x.w); }
};
inline void maximize(int &x, const int &y) {
  if (x < y) x = y;
}
edge e[300300];
int f[300300], mf[300300];
int n, m;
void loadgraph(void) {
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (m); i = i + 1) e[i].input();
  sort(e + 1, e + m + 1);
}
void optimize(void) {
  int j = 1;
  for (int i = (1); i <= (m); i = i + 1) {
    while (j < i && e[j].w < e[i].w) {
      maximize(mf[e[j].v], f[j]);
      j++;
    }
    f[i] = mf[e[i].u] + 1;
  }
  int res = 0;
  for (int i = (1); i <= (m); i = i + 1) maximize(res, f[i]);
  printf("%d", res);
}
int main(void) {
  loadgraph();
  optimize();
  return 0;
}
