#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > wei[300005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  long long dp[n + 1], dp1[n + 1];
  memset(dp, 0, sizeof dp);
  memset(dp1, 0, sizeof dp);
  long long x, y, w;
  pair<long long, long long> p;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y >> w;
    p.first = x;
    p.second = y;
    wei[w].push_back(p);
  }
  for (long long i = 0; i < 300001; i++) {
    for (long long j = 0; j < (long long)wei[i].size(); j++) {
      dp[wei[i][j].second] =
          max(dp[wei[i][j].second], dp1[wei[i][j].first] + 1);
    }
    for (long long j = 0; j < wei[i].size(); j++) {
      dp1[wei[i][j].second] = max(dp[wei[i][j].second], dp1[wei[i][j].second]);
    }
  }
  cout << *max_element(dp1 + 1, dp1 + n + 1);
  return 0;
}
