#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
struct item {
  int to, w;
};
int n, m;
int dp[3][N];
vector<vector<pair<int, int> > > g(N);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int v, u, w;
    cin >> v >> u >> w;
    g[w].push_back({v, u});
  }
  for (int i = 1; i <= N; ++i) {
    for (auto it : g[i]) {
      int v = it.first, u = it.second;
      dp[0][u] = max(dp[1][v] + 1, dp[0][u]);
    }
    for (auto it : g[i]) {
      int u = it.second;
      dp[1][u] = max(dp[1][u], dp[0][u]);
    }
  }
  cout << *max_element(dp[1] + 1, dp[1] + n + 1);
}
