#include <bits/stdc++.h>
using namespace std;
ifstream fin("A.in");
ofstream fout("A.out");
vector<int> I[300010];
int wh[300010], ans[300010], maxv[300010], rez, n, m, u, v, w;
struct edge {
  int u, v, w;
} e[300010];
bool cmp(edge a, edge 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);
    e[i].u = u;
    e[i].v = v;
    e[i].w = w;
  }
  sort(e + 1, e + m + 1, cmp);
  for (int i = 1; i <= m; ++i) {
    I[e[i].v].push_back(i);
  }
  for (int i = 1; i <= m; ++i) {
    int first = e[i].u;
    for (; wh[first] < I[first].size() && e[I[first][wh[first]]].w < e[i].w;
         ++wh[first]) {
      maxv[first] = max(maxv[first], ans[I[first][wh[first]]]);
    }
    ans[i] = maxv[first] + 1;
    rez = max(rez, ans[i]);
  }
  cout << rez;
}
