#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int LOGN = 30;
struct node {
  int l, r, val;
  node() { l = r = val = 0; }
};
int curr, root[N];
node seg[4 * N * LOGN];
void init() {
  curr = 1;
  seg[curr].l = seg[curr].r = seg[curr].val = 0;
}
int nnode(int val, int l, int r) {
  seg[curr].val = val;
  seg[curr].l = l;
  seg[curr].r = r;
  return curr++;
}
int Insert(int lo, int hi, int cur, int idx, int val) {
  if (idx < lo || idx > hi)
    return cur;
  else if (lo == hi)
    return nnode(val, 0, 0);
  int mid = (lo + hi) >> 1;
  int pos = nnode(-1, Insert(lo, mid, seg[cur].l, idx, val),
                  Insert(mid + 1, hi, seg[cur].r, idx, val));
  seg[pos].val = max(seg[seg[pos].l].val, seg[seg[pos].r].val);
  return pos;
}
int query(int lo, int hi, int pos, int l, int r) {
  if (l <= lo && r >= hi)
    return seg[pos].val;
  else if (lo > hi || r < lo || l > hi)
    return 0;
  int mid = (lo + hi) >> 1;
  return max(query(lo, mid, seg[pos].l, l, r),
             query(mid + 1, hi, seg[pos].r, l, r));
}
int U[N], V[N], W[N], dp[N];
vector<int> g[N];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i < m + 1; i++) {
    cin >> U[i] >> V[i] >> W[i];
    ++W[i];
    g[V[i]].push_back(i);
  }
  int ans = 1;
  init();
  for (int i = 1; i < n + 1; i++) root[i] = nnode(0, 0, 0);
  for (int i = 1; i < m + 1; i++) {
    int w = W[i], u = U[i], v = V[i];
    int val = query(1, N - 1, root[u], 0, w - 1);
    root[v] = Insert(1, N - 1, root[v], w, val + 1);
    ans = max(ans, val + 1);
  }
  cout << ans << endl;
  return 0;
}
