#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAXN = 1e6 + 10;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
const long long INF = 8e18;
const int LOG = 22;
long long pw(long long a, long long b, long long mod) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod
                      : pw(a * a % mod, b / 2, mod)));
}
set<pair<long long, long long> > v[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    v[i].insert(make_pair(-1, 0));
  }
  for (int i = 1; i <= m; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    auto it = v[a].lower_bound(make_pair(w, -1));
    it--;
    int cnt = it->second + 1;
    auto It = v[b].lower_bound(make_pair(w + 1, -1));
    It--;
    if (It->second >= cnt) continue;
    while (1) {
      It = v[b].lower_bound(make_pair(w, -1));
      if (It == v[b].end()) break;
      if (It->second > cnt) break;
      v[b].erase(It);
    }
    v[b].insert(make_pair(w, cnt));
  }
  long long Mx = 0;
  for (int i = 1; i <= n; i++) {
    auto it = v[i].end();
    it--;
    Mx = max(Mx, it->second);
  }
  cout << Mx;
  return 0;
}
