#include <bits/stdc++.h>
using namespace std;
int n, m;
struct edge {
  int a, b, w;
} ed[101010];
map<int, int> data[101010];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &ed[i].a, &ed[i].b, &ed[i].w);
  }
  data[ed[1].b].insert(make_pair(ed[1].w, 1));
  int ans = 1;
  for (int i = 2; i <= m; ++i) {
    auto p = data[ed[i].a].lower_bound(ed[i].w);
    int val;
    if (p == data[ed[i].a].begin()) {
      val = 1;
    } else {
      p--;
      val = p->second + 1;
    }
    ans = max(val, ans);
    p = data[ed[i].b].lower_bound(ed[i].w);
    if (p != data[ed[i].b].begin()) {
      p--;
      if (p->second >= val)
        continue;
      else
        p++;
    }
    while (p != data[ed[i].b].end() && p->second <= val) {
      p = data[ed[i].b].erase(p);
    }
    data[ed[i].b].insert(make_pair(ed[i].w, val));
  }
  printf("%d\n", ans);
}
