#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 100000 + 10;
vector<int> val[maxn];
vector<int> ver[maxn];
int n, m, u, v, w;
void re(int o) {
  ver[v].erase(ver[v].begin() + o);
  val[v].erase(val[v].begin() + o);
}
void pu(int a, int b, int c) {
  val[v].push_back(b);
  ver[v].push_back(c);
}
struct he {
  int from, to, w;
} HMM[maxn];
bool cmp(he a, he b) { return a.w < b.w; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    HMM[i].from = u;
    HMM[i].to = v;
    HMM[i].w = w;
  }
  int over = 0;
  sort(HMM + 1, HMM + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    u = HMM[i].from;
    v = HMM[i].to;
    w = HMM[i].w;
    int ks = 1;
    for (int j = 0; j < val[u].size(); j++) {
      int t = val[u][j];
      int st = ver[u][j];
      if (t < w && st + 1 > ks) {
        ks = st + 1;
      }
    }
    if (val[v].size() == 2) {
      if (w != val[v][0] && ks > ver[v][1]) {
        if (ver[v][0] < ver[v][1]) {
          re(0);
          pu(u, w, ks);
        } else {
          re(1);
          pu(u, w, ks);
        }
      } else if (ks > ver[v][1]) {
        re(1);
        pu(u, w, ks);
      }
    } else {
      pu(u, w, ks);
    }
    over = max(over, ks);
  }
  printf("%d\n", over);
  return 0;
}
