#include <bits/stdc++.h>
using namespace std;
void fastStream() {
  cin.tie(0);
  std::ios_base::sync_with_stdio(0);
}
struct edge {
  int to, cost, id, from;
  edge(int f, int t, int c, int d) {
    from = f;
    to = t;
    cost = c;
    id = d;
  }
  edge(){};
};
int N, M;
edge es[1000100];
int maxs[400000];
int main() {
  scanf("%d%d", &N, &M);
  map<int, vector<int> > m;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    es[i] = edge(a, b, c, i);
    m[-c].push_back(i);
  }
  int res = 0;
  for (pair<int, vector<int> > p : m) {
    vector<int> v = p.second;
    vector<pair<int, int> > updates;
    for (int id : v) {
      updates.push_back(
          make_pair(es[id].from, max(maxs[es[id].from], maxs[es[id].to] + 1)));
    }
    for (pair<int, int> p : updates) {
      int a = p.first;
      int b = p.second;
      maxs[a] = max(maxs[a], b);
      res = max(res, maxs[a]);
    }
  }
  cout << res << endl;
  return 0;
}
