#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;

class segtree{
public:
	int N;
	vector<int> dp;
	segtree(){}

	void init(int n_){
		N=n_;
		dp.clear();
		dp.resize(N+1,0);
	}
	void update(int k,int v){
		k++;
		while(k<=N){
			dp[k]=max(dp[k],v);
			k+=k&-k;
		}
	}

	int query(int b){
		int res=0;
		while(b>0){
			res=max(res,dp[b]);
			//printf("%d %d %d\n",b,N,res);
			b-=b&-b;
		}
		return res;
	}
};

class segtree2D{
public:
	int N;
	segtree seg[1<<20];
	vector<int> id[1<<20];
	segtree2D(){}

	void setN(int n_){
		N=1;
		while(N<n_){
			N*=2;
		}
		for(int i=0;i<N*2;i++){
			id[i].clear();
		}
	}

	void init(){
		for(int i=0;i<N*2;i++){
			sort(id[i].begin(),id[i].end());
			id[i].erase(unique(id[i].begin(),id[i].end()),id[i].end());
			seg[i].init(id[i].size());
		}
	}

	void insertnode(int y,int z){
		y+=N-1;
		id[y].push_back(z);
		while(y>0){
			y=(y-1)/2;
			id[y].push_back(z);
		}
	}	
	void update(int y,int z,int v){
		y+=N-1;
		int pos=lower_bound(id[y].begin(),id[y].end(),z)-id[y].begin();
		seg[y].update(pos,v);
		while(y>0){
			y=(y-1)/2;
			int pos=lower_bound(id[y].begin(),id[y].end(),z)-id[y].begin();
			seg[y].update(pos,v);
		}
	}

	int query(int b,int b2,int k,int l,int r){
		if(id[k].size()==0)return 0;
		if(b<=l)return 0;
		if(r<=b){
			int v=lower_bound(id[k].begin(),id[k].end(),b2)-id[k].begin();
			if(v==0)return 0;
			return seg[k].query(v);
		}
		int mid=(l+r)/2;
		int vl=query(b,b2,k*2+1,l,mid);
		int vr=query(b,b2,k*2+2,mid,r);
		return max(vl,vr);
	}

	int query(int b,int b2){
		return query(b,b2,0,0,N);
	}
};

int m,n,a,b;
int C=~(1<<31),M=(1<<16)-1;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}


segtree2D seg;
int x[300005],y[300005],z[300005];

vector<P> vec[300005];

vector<int> xi;
vector<int> yi;
vector<int> zi;

void solve(){
	for(int i=0;i<xi.size();i++){
		vec[i].clear();
	}
	xi.clear();
	yi.clear();
	zi.clear();
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&x[i],&y[i],&z[i]);
		xi.push_back(x[i]);
		yi.push_back(y[i]);
		zi.push_back(z[i]);	
	}
	for(int i=0;i<n;i++){
		x[m+i]=r();
		y[m+i]=r();
		z[m+i]=r();
		xi.push_back(x[m+i]);
		yi.push_back(y[m+i]);
		zi.push_back(z[m+i]);
	}
	sort(xi.begin(),xi.end());
	xi.erase(unique(xi.begin(),xi.end()),xi.end());
	sort(yi.begin(),yi.end());
	yi.erase(unique(yi.begin(),yi.end()),yi.end());
	sort(zi.begin(),zi.end());
	zi.erase(unique(zi.begin(),zi.end()),zi.end());
	seg.setN(yi.size());
	for(int i=0;i<n+m;i++){
		x[i]=lower_bound(xi.begin(),xi.end(),x[i])-xi.begin();
		y[i]=lower_bound(yi.begin(),yi.end(),y[i])-yi.begin();
		z[i]=lower_bound(zi.begin(),zi.end(),z[i])-zi.begin();
		vec[x[i]].push_back(P(y[i],z[i]));
		seg.insertnode(y[i],z[i]);
	}
	seg.init();
	for(int i=0;i<xi.size();i++){
		sort(vec[i].begin(),vec[i].end());
		reverse(vec[i].begin(),vec[i].end());
		for(int j=0;j<vec[i].size();j++){
			P p=vec[i][j];
			int v=seg.query(p.first,p.second);
			//printf("%d %d %d %d\n",i,p.first,p.second,v);
			seg.update(p.first,p.second,v+1);
		}
	}
	printf("%d\n",seg.query(yi.size(),zi.size()));
}

int main(void){
	while(1){
		scanf("%d%d%d%d",&m,&n,&a,&b);
		//printf("%d %d %d %d\n",m,n,a,b);
		if(m==0 && n==0 && a==0 && b==0)break;
		solve();
	}
	return 0;
}


Measured memory_bytes: 170128