#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
const int N = 100005;
int n, m;
struct node {
  int val;
  node *l, *r;
  node() {
    l = r = NULL;
    val = 0;
  }
};
node *tr[N];
int query(node *cur, int s, int e, int l, int r) {
  if (cur == NULL || s > r || e < l)
    return 0;
  else if (l <= s && e <= r)
    return cur->val;
  int m = (s + e) >> 1;
  return max(query(cur->l, s, m, l, r), query(cur->r, m + 1, e, l, r));
}
void update(node *cur, int s, int e, int idx, int val) {
  if (s == e) {
    cur->val = max(cur->val, val);
    return;
  }
  int m = (s + e) >> 1;
  if (idx <= m) {
    if (cur->l == NULL) cur->l = new node();
    update(cur->l, s, m, idx, val);
    cur->val = max(cur->val, cur->l->val);
  } else {
    if (cur->r == NULL) cur->r = new node();
    update(cur->r, m + 1, e, idx, val);
    cur->val = max(cur->val, cur->r->val);
  }
}
signed main() {
  scanf("%d %d", &n, &m);
  int ans = 1;
  for (int i = 1; i <= n; i++) tr[i] = new node();
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    int val = query(tr[u], 0, 100000, 0, w - 1);
    ans = max(ans, val + 1);
    update(tr[v], 0, 100000, w, val + 1);
  }
  printf("%d", ans);
  return 0;
}
