#include <bits/stdc++.h>
const long long INF = (long long)1e18 + 123;
const int inf = (int)2e9 + 123;
const int mod = 1e9 + 7;
using namespace std;
int n, m, ans = 0;
int d[300011], old[300011], nw[300011];
struct edge {
  int from, to, w;
  bool u;
  edge() {
    from = to = 0;
    w = u = 0;
  }
  edge(int x, int y, int z) {
    from = x;
    to = y;
    w = z;
    u = 0;
  }
};
vector<edge> e;
bool cmp(edge x, edge y) { return x.w < y.w; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    e.push_back(edge(x, y, w));
  }
  sort(e.begin(), e.end(), &cmp);
  e.push_back(edge(0, 0, inf));
  for (int i = 0; i <= m; i++) {
    if (i > 0 && e[i].w > e[i - 1].w) {
      int j = i - 1;
      while (j >= 0 && e[j].w == e[i - 1].w) {
        old[e[j].to] = max(old[e[j].to], nw[e[j].to]);
        nw[e[j].to] = 0;
        ans = max(ans, old[e[j].to]);
        j--;
      }
    }
    nw[e[i].to] = max(nw[e[i].to], old[e[i].from] + 1);
  }
  cout << ans;
  return 0;
}
