#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 7;
vector<pair<long long, long long> > g[N];
vector<long long> f[N];
long long n, m, i, u, v, w, z, mx;
long long dpf(long long u, long long k = 0, long long d = 0) {
  long long j;
  j = d ? k
        : upper_bound(g[u].begin(), g[u].end(), make_pair(k, N)) - g[u].begin();
  long long &R = f[u][j];
  if (R != -1) return R;
  auto [w, v] = g[u][j];
  long long r1, r2;
  R = 0;
  r1 = dpf(u, j + 1, 1);
  r2 = dpf(v, w, 0) + 1;
  R = max(r1, r2);
  return R;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> u >> v >> w;
    g[u].push_back(make_pair(w, v));
  }
  for (i = 1; i <= n; i++) {
    g[i].push_back(make_pair(1e15, 0));
    sort(g[i].begin(), g[i].end());
    z = g[i].size();
    f[i].assign(z, -1);
    f[i].back() = 0;
  }
  for (i = 1; i <= n; i++) mx = max(mx, dpf(i));
  cout << mx << "\n";
}
