#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define FILL(x,y) memset(x,y,sizeof(x))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define x first
#define y second
#define st first
#define nd second
#define pb push_back
#define nleft (n<<1)
#define nright (nleft+1)
#define lsb(a) (a&-a)
#include<vector>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<set>
#include<map>
#include<queue>
using namespace std;
#ifndef ONLINE_JUDGE
#include<fstream>
ifstream cin("cutii.in");
ofstream cout("cutii.out");
#else
#include<iostream>
#endif
const int NMAX = 3510;
const int sep = 200000;
const int INF = 0x3f3f3f3f;
const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};
typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef pair<K, K> PKK;
typedef vector<int> VI;
struct triple{
int x,y,z;
};
triple A[NMAX];
int bit[NMAX][NMAX];
bool cmp(triple l, triple r)
{
return l.z<r.z;
}
int query(int x, int y)
{
int m=0;
for(int i=x; i>0; i-=lsb(i))
{
for(int j=y; j>0; j-=lsb(j))
{
m=max(m, bit[i][j]);
}
}
return m;
}
void update(int x, int y, int val)
{
for(int i=x; i<=NMAX; i+=lsb(i))
{
for(int j=y; j<=NMAX; j+=lsb(j))
{
bit[i][j]=max(val, bit[i][j]);
}
}
}
void init(int x)
{
for(int i=x; i<=NMAX; i+=lsb(i))
{
for(int j=x; j<=NMAX; j+=lsb(j))
{
bit[i][j]=min(0, bit[i][j]);
}
}
}
int n,t;
int main()
{
cin>>n;
for(cin>>t; t; t--){
for(int i=1; i<=n; ++i)
{
init(i);
cin>>A[i].x>>A[i].y>>A[i].z;
}
sort(A+1,A+n+1,cmp);
for(int i=1; i<=n; ++i)
{
update(A[i].x, A[i].y, query(A[i].x-1, A[i].y-1) + 1);
}
cout<<query(A[n].x, A[n].y)<<"\n";
}
}