Problem: Let us compare two triples a = (xa, ya, za) and b = (xb, yb, zb) by a partial order ∠ defined as follows.
a ∠ b ⇔ xa < xb and ya < yb and za < zb



Your mission is to find, in the given set of triples, the longest ascending series a1 ∠ a2 ∠ ... ∠ ak.



Input

The input is a sequence of datasets, each specifying a set of triples formatted as follows.


m n A B
x1 y1 z1
x2 y2 z2
...
xm ym zm


Here, m, n, A and B in the first line, and all of xi, yi and zi (i = 1, . . . , m) in the following lines are non-negative integers.

Each dataset specifies a set of m + n triples. The triples p1 through pm are explicitly specified in the dataset, the i-th triple pi being (xi, yi, zi). The remaining n triples are specified by parameters A and B given to the following generator routine.


int a = A, b = B, 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;
}


Repeated 3n calls of r() defined as above yield values of xm+1, ym+1, zm+1, xm+2, ym+2, zm+2, ..., xm+n, ym+n, and zm+n, in this order.

You can assume that 1 ≤ m + n ≤ 3 × 105, 1 ≤ A,B ≤ 216, and 0 ≤ xk, yk, zk < 106 for 1 ≤ k ≤ m + n.

The input ends with a line containing four zeros. The total of m + n for all the datasets does not exceed 2 × 106.

Output

For each dataset, output the length of the longest ascending series of triples in the specified set. If pi1 ∠ pi2 ∠ ... ∠ pik is the longest, the answer should be k.

Example

Input

6 0 1 1
0 0 0
0 2 2
1 1 1
2 0 2
2 2 0
2 2 2
5 0 1 1
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
10 0 1 1
3 0 0
2 1 0
2 0 1
1 2 0
1 1 1
1 0 2
0 3 0
0 2 1
0 1 2
0 0 3
0 10 1 1
0 0 0 0


Output

3
5
1
3

Solution (in CPP):

Solution:
#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;
}