#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
const int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int f[300005][5];
vector<pair<int, int>> edges[100005];
set<int> weights;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int u, v, w;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> w;
    edges[w].push_back({u, v});
    weights.insert(w);
  }
  for (auto w : weights) {
    for (auto edge : edges[w]) {
      f[edge.second][2] = max(f[edge.second][2], f[edge.first][1] + 1);
    }
    for (auto edge : edges[w]) {
      f[edge.second][1] = max(f[edge.second][1], f[edge.second][2]);
      f[edge.second][2] = 0;
    }
  }
  int ans = 0;
  for (int v = 1; v <= n; v++) {
    ans = max(ans, f[v][1]);
  }
  cout << ans << '\n';
  return 0;
}
