#include <bits/stdc++.h>
using namespace std;
const int maxn = 300003;
int dp[maxn][2];
int maxi[maxn];
pair<int, pair<int, int> > w[maxn];
void add(int v, int len, int ans) {
  if (len > maxi[v]) {
    dp[v][1] = max(dp[v][1], dp[v][0]);
    dp[v][0] = ans;
    maxi[v] = len;
  } else {
    dp[v][0] = max(dp[v][0], ans);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, len;
    scanf("%d%d%d", &u, &v, &len);
    w[i] = make_pair(len, make_pair(u, v));
  }
  sort(w + 1, w + m + 1);
  for (int i = 1; i <= m; i++) {
    int len = w[i].first, u = w[i].second.first, v = w[i].second.second;
    if (maxi[u] < len)
      add(v, len, max(dp[u][0], dp[u][1]) + 1);
    else
      add(v, len, dp[u][1] + 1);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) ans = max(ans, max(dp[i][0], dp[i][1]));
  cout << ans << "\n";
  return 0;
}
