#include <bits/stdc++.h>
#pragma GCC optimize(2)
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
long long read() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 3e5 + 10;
struct eg {
  int u, v, wi;
  bool operator<(const eg temp) const { return wi < temp.wi; }
} edge[N];
int dp[N][2];
int n, m;
stack<eg> st;
int main() {
  n = read(), m = read();
  int u, v, wi;
  for (int i = 1; i <= m; i++) {
    u = read(), v = read(), wi = read();
    edge[i].u = u;
    edge[i].v = v;
    edge[i].wi = wi;
  }
  sort(edge + 1, edge + 1 + m);
  for (int i = 1; i <= m;) {
    st.push(edge[i]);
    u = edge[i].u, v = edge[i].v, wi = edge[i].wi;
    dp[v][1] = dp[u][0] + 1;
    int j;
    for (j = i + 1; j <= m && edge[j - 1].wi == edge[j].wi; j++) {
      st.push(edge[j]);
      u = edge[j].u, v = edge[j].v, wi = edge[j].wi;
      dp[v][1] = max(dp[v][1], dp[u][0] + 1);
    }
    i = j;
    while (!st.empty()) {
      eg top = st.top();
      v = top.v;
      dp[v][0] = max(dp[v][1], dp[v][0]);
      dp[v][1] = 0;
      st.pop();
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i][0]);
  }
  cout << ans << endl;
  return 0;
}
