#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
struct node {
  int u, v, w;
  bool operator<(const node& ss) const { return w < ss.w; }
} e[300110];
int n, m;
int dp[300110], g[300110];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) cin >> e[i].u >> e[i].v >> e[i].w;
  sort(e, e + m);
  int t = 0, ans = 0;
  for (int i = 0; i < m; ++i) {
    dp[i] = g[e[i].u] + 1;
    if (e[i].w != e[i + 1].w) {
      for (int j = t; j <= i; ++j) g[e[j].v] = max(dp[j], g[e[j].v]);
      t = i + 1;
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
