#include <fstream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
struct cutie {
int x, y, z;
};
ifstream fin;
ofstream fout;
bool cmp(cutie c1, cutie c2) {
return (c1.x < c2.x);
}
int interogare(vector <vector <int>> &aib, int l, int c) {
int maxim = 0;
while (l != 0) {
int cc = c;
while (cc != 0) {
maxim = max(maxim, aib[l][cc]);
int p2 = (cc & -cc);
cc -= p2;
}
int p2 = (l & -l);
l -= p2;
}
return maxim;
}
void actualizare(vector <vector <int>> &aib, int n, int l, int c, int val) {
while (l <= n) {
int cc = c;
while (cc <= n) {
aib[l][cc] = max(aib[l][cc], val);
int p2 = (cc & -cc);
cc += p2;
}
int p2 = (l & -l);
l += p2;
}
}
void test(int n) {
vector <cutie> v(n);
for (int i = 0; i < n; i++) {
fin >> v[i].x >> v[i].y >> v[i].z;
}
sort(v.begin(), v.end(), cmp);
vector <vector <int>> aib(n + 1, vector <int>(n + 1));
int lung_max = 0;
for (int i = 0; i < n; i++) {
int l = interogare(aib, v[i].y - 1, v[i].z - 1);
int lung_c = l + 1;
lung_max = max(lung_max, lung_c);
actualizare(aib, n, v[i].y, v[i].z, lung_c);
}
fout << lung_max << "\n";
}
int main() {
fin.open("cutii.in");
fout.open("cutii.out");
int n, t;
fin >> n >> t;
for (int i = 0; i < t; i++) {
test(n);
}
fin.close();
fout.close();
return 0;
}