#include<bits/stdc++.h>
using namespace std;
FILE* fin=fopen("cutii.in","r");
FILE* fout=fopen("cutii.out","w");
const short NMAX = 3501;
short aib[NMAX][NMAX],n,lastupdated[NMAX][NMAX];
struct cutie{short x, y, z;};
bool cmp(cutie a, cutie b){
return a.x<b.x || (a.x==b.x && (a.y<b.y || (a.y==b.y && a.z<b.z)));
}
short lastBit(short num){
return num&-num;
}
void insert2(short pos1, short pos2, short val, short curr){
while(pos2<NMAX){
if(lastupdated[pos1][pos2]<curr-1)
aib[pos1][pos2]=0;
aib[pos1][pos2]=max(aib[pos1][pos2],val);
lastupdated[pos1][pos2]=curr;
pos2+=lastBit(pos2);
}
}
void insert(short pos1,short pos2, short val, short curr){
while(pos1<NMAX){
insert2(pos1,pos2,val, curr);
pos1+=lastBit(pos1);
}
}
short getmax2(short pos1,short pos2, short curr){
short Max=0;
while(pos2){
if(lastupdated[pos1][pos2]>=curr-1)
Max=max(Max,aib[pos1][pos2]);
else{
aib[pos1][pos2]=0;
lastupdated[pos1][pos2]=curr;
}
pos2-=lastBit(pos2);
}
return Max;
}
short getmax(short pos1,short pos2, short curr){
short Max=0;
while(pos1){
Max=max(Max,getmax2(pos1,pos2, curr));
pos1-=lastBit(pos1);
}
return Max;
}
void tc(){
short dp[NMAX]{},ans=0;
cutie v[NMAX];
for(short i=0;i<n;i++){
fscanf(fin,"%hd%hd%hd",&v[i].x,&v[i].y,&v[i].z);
for(short j=0;j<n;j++)
aib[i+1][j+1]=0;
}
sort(v,v+n,cmp);
for(short i=0;i<n;i++){
dp[i]=getmax(v[i].y-1,v[i].z-1, i)+1;
insert(v[i].y,v[i].z,dp[i], i);
ans=max(ans,dp[i]);
}
fprintf(fout,"%hd \n",ans);
}
int main()
{
short k;
fscanf(fin,"%hd%hd",&n,&k);
while(k--)
tc();
return 0;
}