#include <stdio.h>
#include <stdlib.h>
#define zero(x) (x&(-x))
#define max(a, b) a>b?a:b
int aib[3502][3502];
struct cutie{
int x, y, z;
} v[3501], aux;
inline int getMax(int y, int z){
int aux, rez=0;
for(aux=z; y>0; y-=zero(y), aux=z)
for(aux; aux>0; aux-=zero(aux))
rez=max(rez, aib[y][aux]);
return rez;
}
inline void update(int y, int z, int val, int n){
int aux;
for(aux=z; y<=n; y+=zero(y), aux=z)
for(aux; aux<=n; aux+=zero(aux))
aib[y][aux]=max(val, aib[y][aux]);
}
void myqsort(int begin, int end){
int b=begin, e=end, pivot=v[begin+rand()%(end-begin+1)].x;
while(b<=e){
while(v[b].x<pivot)
++b;
while(v[e].x>pivot)
--e;
if(b<=e){
aux=v[b]; v[b]=v[e]; v[e]=aux;
++b; --e;
}
}
if(begin<e)
myqsort(begin, e);
if(b<end)
myqsort(b, end);
}
int main()
{
FILE *fin, *fout;
int n, t, i, j, a, b, maxim;
fin=fopen("cutii.in", "r");
fscanf(fin, "%d%d", &n, &t);
fout=fopen("cutii.out", "w");
for(j=0; j<t; j++){
maxim=0;
for(i=1; i<=n; i++)
fscanf(fin, "%d%d%d", &v[i].x, &v[i].y, &v[i].z);
myqsort(1, n);
for(i=1; i<=n; i++){
a=getMax(v[i].y, v[i].z)+1;
update(v[i].y, v[i].z, a, n+1);
maxim=max(a, maxim);
}
fprintf(fout, "%d\n", maxim);
for(i=1; i<=n; i++)
for(a=v[i].y; a<=n+1; a+=zero(a))
for(b=v[i].z; b<=n+1; b+=zero(b))
aib[a][b]=0;
}
fclose(fin);
fclose(fout);
return 0;
}