#include <bits/stdc++.h>
#include <array>
using namespace std;
#if 1
#define cin fin
#define cout fout
ifstream fin("cutii.in");
ofstream fout("cutii.out");
#endif // 1
int n;
const int nmx = 3505;
const int inf = 1e7;
typedef array<int, 3> C;
vector<C> cut;
bool canFit(C c1, C c2) { // can box c1 fit in box c2 ?
return c1[0] < c2[0] && c1[1] < c2[1] && c1[2] < c2[2];
}
int bit[nmx][nmx];
void update(int x, int y, int val) {
for (int i = x; i <= n; i += i & -i)
for (int j = y; j <= n; j += j & -j)
bit[i][j] = max(bit[i][j], val);
}
int query(int x, int y) {
int res = 0;
for (int i = x; i >= 1; i -= i & -i)
for (int j = y; j >= 1; j -= j & -j)
res = max(res, bit[i][j]);
return res;
}
void solve() {
vector<int> dp(n, 1);
cut.clear();
for (int i = 0; i < n; i++) {
C c;
cin >> c[0] >> c[1] >> c[2];
cut.push_back(c);
}
sort(cut.begin(), cut.end());
vector<int> ev = { 0 }; // stidx
for (int i = 1; i < n; i++)
if (cut[ev.back()][0] != cut[i][0])
ev.push_back(i);
ev.push_back(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
bit[i][j] = 0;
int res = 0;
for (int i = 0; i < ev.size() - 1; i++) {
// compute dp[i]
for (int j = ev[i]; j < ev[i + 1]; j++)
dp[j] = query(cut[j][1] - 1, cut[j][2] - 1) + 1, res = max(res, dp[j]);
for (int j = ev[i]; j < ev[i + 1]; j++)
update(cut[j][1], cut[j][2], dp[j]);
}
cout << res << "\n";
}
int main() {
int t;
cin >> n >> t;
while (t--) {
solve();
}
}