#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, y = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') y = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * y;
}
void put(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) put(x / 10);
  putchar((x % 10) + 48);
}
long long n, m, f[300010], g[300010];
vector<long long> lend[300010];
struct Line {
  long long u, v, dis;
  Line() { u = v = dis = 0; }
} a[300010];
inline bool cmp(const Line& A, const Line& B) { return A.dis < B.dis; }
void solve(long long l, long long r) {
  for (long long i = l; i <= r; i++) g[a[i].v] = max(g[a[i].v], f[a[i].u] + 1);
  long long w = a[l].dis;
  for (long long i = 0; i < lend[w].size(); i++) {
    f[lend[w][i]] = max(f[lend[w][i]], g[lend[w][i]]);
    g[lend[w][i]] = 0;
  }
}
signed main() {
  n = read();
  m = read();
  for (long long i = 1; i <= m; i++) {
    a[i].u = read();
    a[i].v = read();
    a[i].dis = read();
    lend[a[i].dis].push_back(a[i].v);
  }
  sort(a + 1, a + m + 1, cmp);
  long long i = 1, j = 1;
  while (i <= m) {
    while (j <= m && a[j].dis == a[i].dis) j++;
    j--;
    solve(i, j);
    j++;
    i = j;
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) ans = max(ans, f[i]);
  cout << ans << "\n";
  fclose(stdin);
  fclose(stdout);
  return 0;
}
