#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
int cnt;
int tr[N], ls[N], rs[N], rt[N];
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
inline void insert(int l, int r, int p, int z, int &x) {
  if (!x) x = ++cnt;
  tr[x] = max(tr[x], z);
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (p <= mid)
    insert(l, mid, p, z, ls[x]);
  else
    insert(mid + 1, r, p, z, rs[x]);
}
inline int query(int l, int r, int R, int x) {
  if (!x) return 0;
  if (r <= R) return tr[x];
  int mid = (l + r) >> 1, ret = query(l, mid, R, ls[x]);
  if (R > mid) ret = max(ret, query(mid + 1, r, R, rs[x]));
  return ret;
}
int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x = gi(), y = gi(), z = gi(), k;
    if (!z)
      k = 1;
    else
      k = query(0, 100000, z - 1, rt[x]) + 1;
    ans = max(ans, k);
    insert(0, 100000, z, k, rt[y]);
  }
  cout << ans;
  return 0;
}
