#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 50;
int dp[N];
vector<pair<int, pair<int, int>>> g;
int main(int argc, const char **argv) {
  if (argc >= 2) {
    freopen(argv[1], "r", stdin);
  }
  int m, n;
  cin >> n >> m;
  for (int i = 0, x, y, z; i < m; i++) {
    scanf("%d %d %d", &x, &y, &z);
    g.push_back({z, {x, y}});
  }
  sort(g.begin(), g.end());
  for (int i = 0; i < g.size();) {
    vector<pair<int, int>> v;
    int p = g[i].first;
    v.push_back({g[i].second.second, dp[g[i].second.first]});
    i++;
    while (i < g.size() && g[i].first == p) {
      v.push_back({g[i].second.second, dp[g[i].second.first]});
      i++;
    }
    for (auto u : v) {
      dp[u.first] = max(dp[u.first], u.second + 1);
    }
  }
  cout << *max_element(dp + 1, dp + 1 + n) << endl;
}
