#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("cutii.in");
ofstream cout("cutii.out");
class AIB2d{
public:
AIB2d(int n) : n{n}, aib{vector<vector<int>>(n+1)}
{
for(int i=1;i<=n;i++)
aib[i]=vector<int>(n+1,0);
}
void update(int x,int y,int v)
{
for(int i=x;i<=n;i+=i&-i)
for(int j=y;j<=n;j+=j&-j)
aib[i][j]=max(aib[i][j],v);
}
int query(int x,int y)
{
int ans=0;
for(int i=x;i>=1;i-=i&-i)
for(int j=y;j>=1;j-=j&-j)
ans=max(ans,aib[i][j]);
return ans;
}
void cleanup()
{
for(int i=1;i<=n;i++)
aib[i]=vector<int>(n+1,0);
}
private:
vector<vector<int>> aib;
int n;
};
struct idt
{
int x,y,z;
const bool operator<(idt const& other) const
{
return z<other.z || (z==other.z && x<other.x) || (z==other.z && x==other.x
&& y<other.y);
}
};
int n,test,dp[3505];
idt A[3505];
int main()
{
cin>>n>>test;
AIB2d aib(n);
while(test--)
{
for(int i=1;i<=n;i++)
cin>>A[i].x>>A[i].y>>A[i].z;
sort(A+1,A+n+1);
int ans=0;
for(int i=1;i<=n;i++)
{
dp[i]=1+aib.query(A[i].x-1,A[i].y-1);
aib.update(A[i].x,A[i].y,dp[i]);
ans=max(ans,dp[i]);
}
cout<<ans<<"\n";
aib.cleanup();
}
return 0;
}