#include <bits/stdc++.h>
using namespace std;
vector<map<int, int> > ve;
int get_edge_len(int a, int w) {
  auto it = ve[a].lower_bound(w);
  if (it == ve[a].begin()) {
    return 1;
  }
  return ((--it)->second) + 1;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  ve.resize(n + 1);
  int ans = 0;
  while (m--) {
    int a, b, w;
    scanf("%d %d %d", &a, &b, &w);
    int val = get_edge_len(a, w);
    if (get_edge_len(b, w + 1) > val) {
      continue;
    }
    ve[b][w] = max(ve[b][w], val);
    auto it = ve[b].upper_bound(w);
    while (it != ve[b].end() && it->second <= val) {
      it = ve[b].erase(it);
    }
    ans = max(ans, val);
  }
  printf("%d\n", ans);
  return 0;
}
