#include <bits/stdc++.h>
using namespace std;
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long powe(long long a, int b) {
  long long ret = 1LL;
  while (b) {
    if (b & 1) ret = (ret * a * 1LL) % 1000000007;
    a = (a * a * 1LL) % 1000000007;
    b >>= 1;
  }
  return ret;
}
struct node {
  int u, v;
};
bool comp(node a, node b) {
  if (a.u == b.u) return a.v < b.v;
  return a.u < b.u;
}
int main() {
  int t, m, n, i, j, k, l;
  t = 1;
  while (t--) {
    scanf("%d", &n);
    scanf("%d", &m);
    vector<pair<int, pair<int, int> > > v;
    for (i = 0; i < m; i++) {
      scanf("%d", &j);
      scanf("%d", &k);
      scanf("%d", &l);
      v.push_back(make_pair(l, make_pair(j, k)));
    }
    sort(v.begin(), v.end());
    int dp[n + 9];
    memset(dp, 0, sizeof(dp));
    stack<pair<int, int> > st[n + 3];
    int ans = 0;
    for (i = 0; i < m; i++) {
      int u = v[i].second.first;
      int vv = v[i].second.second;
      int w = v[i].first;
      vector<pair<int, int> > h;
      while (st[u].size() && st[u].top().second == w) {
        pair<int, int> s = st[u].top();
        st[u].pop();
        h.push_back(s);
      }
      if (st[u].size())
        dp[vv] = max(dp[vv], st[u].top().first + 1);
      else
        dp[vv] = 1;
      for (j = 0; j < h.size(); j++) st[u].push(h[h.size() - 1 - j]);
      if (st[vv].size() && dp[vv] > st[vv].top().first) {
        while (st[vv].size() && st[vv].top().second == w) st[vv].pop();
        st[vv].push(make_pair(dp[vv], w));
      }
      if (!st[vv].size()) st[vv].push(make_pair(dp[vv], w));
      ans = max(ans, dp[vv]);
    }
    cout << ans << "\n";
  }
  return 0;
}
