#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 113;
long long dp[2][N];
signed main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  map<long long, vector<pair<long long, long long>>> mp;
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    mp[c].push_back({a, b});
  }
  long long maxans = 0;
  for (auto i : mp) {
    vector<long long> usd;
    for (auto j : i.second)
      dp[1][j.second] = max(dp[1][j.second], dp[0][j.first] + 1),
      usd.push_back(j.second);
    for (auto j : usd)
      dp[0][j] = max(dp[0][j], dp[1][j]), maxans = max(maxans, dp[0][j]);
  }
  cout << maxans << '\n';
}
