#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
using namespace std;
const int N = 200010;
const int inf = 1000000000;
int n, m, dp[N] = {};
struct node {
  struct node* left;
  struct node* right;
  int a;
  node() : left(NULL), right(NULL), a(0) {}
} * v[N];
int query(node* k, int l, int r, int ql, int qr) {
  if (!k || l > r || l > qr || r < ql) return 0;
  if (l >= ql && r <= qr) return k->a;
  int mid = (l + r) / 2;
  return max(query(k->left, l, mid, ql, qr),
             query(k->right, mid + 1, r, ql, qr));
}
void update(node* k, int l, int r, int target, int val) {
  if (target > r || target < l) return;
  if (l == r) {
    k->a = max(val, k->a);
    return;
  }
  int mid = (l + r) / 2;
  if (!k->left) k->left = new node;
  if (!k->right) k->right = new node;
  update(k->left, l, mid, target, val);
  update(k->right, mid + 1, r, target, val);
  k->a = max(k->left->a, max(k->a, k->right->a));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) v[i] = new node;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dp[i] = query(v[a], 0, N, 0, c - 1) + 1;
    update(v[b], 0, N, c, dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans << '\n';
  return 0;
}
