#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const long double PI = acos(-1);
const long long M = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long mod(long long n, long long m = M) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long exp(long long n, long long k, long long m = M) {
  if (k == 0) return 1;
  if (k == 1) return n;
  long long ax = exp(n, k / 2, m);
  ax = mod(ax * ax, m);
  if (k % 2) ax = mod(ax * n, m);
  return ax;
}
long long gcd(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
void solution(long long testcase) {
  long long n, m;
  cin >> n >> m;
  map<long long, vector<pair<long long, long long> > > col;
  vector<long long> dp(n + 1, 0);
  for (long long i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    col[w].emplace_back(u, v);
  }
  for (auto t : col) {
    vector<pair<long long, long long> > updates;
    for (pair<long long, long long> p : t.second) {
      updates.emplace_back(p.second, dp[p.first] + 1);
    }
    for (pair<long long, long long> p : updates) {
      dp[p.first] = max(dp[p.first], p.second);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  for (long long _testcase = 1; _testcase <= t; _testcase++)
    solution(_testcase);
}
