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 <cstdio>
#include <algorithm>

using namespace std;

const int N = 1000000 + 100;

struct Node{
	int x, y, z;
	void read(){
		scanf("%d%d%d", &x, &y, &z);
	}
	void print(){
		printf("(%d, %d, %d)\n", x, y, z);
	}
};

bool operator <(const Node &a, const Node &b){
	if (a.x < b.x) return true;
	else if (a.x > b.x) return false;
	return a.y > b.y;
}

bool ok = false;

int n, m;
int a, b, C, M;
Node v[N];

int ans[N];
int id[N];
int tmp[N];

int tot;
int t[N];
int cnt[N];


bool cmp(const int &a, const int &b){
	return v[a] < v[b];
}

void insert(int x, int val){
	for (; x < N; x += x & -x){
		if (cnt[x] != tot){
			cnt[x] = tot;
			t[x] = 0;
		}
		t[x] = max(t[x], val);
	}
}

int ask(int x){
	int ret = 0;
	for(; x > 0; x -= x & -x){
		if (cnt[x] != tot){
			cnt[x] = tot;
			t[x] = 0;
		}
		ret = max(ret, t[x]);
	}
	return ret;
}

int r(){
	a = 36969 * (a & M) + (a >> 16);
	b = 18000 * (b & M) + (b >> 16);
	return (C & ((a << 16) + b)) % 1000000;
}

void base_sort(int l, int r){
	int mid = (l + r) >> 1;
	int p1 = l, p2 = mid;
	int pos = l;
	while(p1 < mid && p2 < r){
		if (v[id[p1]].y < v[id[p2]].y){
			tmp[pos++] = id[p1++];
		}
		else{
			tmp[pos++] = id[p2++];
		}
	}
	while(p1 < mid){
		tmp[pos++] = id[p1++];
	}
	while(p2 < r){
		tmp[pos++] = id[p2++];
	}
}

void calc(int a[], int n, int b[], int m){
	tot++;
	int p1 = 0, p2 = 0;
	while(p1 < n && p2 < m){
		if (v[a[p1]].y < v[b[p2]].y){
			insert(v[a[p1]].z, ans[a[p1]]);
			p1++;
		}
		else{
			ans[b[p2]] = max(ans[b[p2]], 1 + ask(v[b[p2]].z - 1));
			p2++;
		}
	}
	while(p2 < m){
		ans[b[p2]] = max(ans[b[p2]], 1 + ask(v[b[p2]].z - 1));
		p2++;
	}
}

void work(int l, int r, bool flag){
	if (l + 1 == r) return;
	int mid = (l + r) >> 1;
	sort(id + l, id + r, cmp);
	work(l, mid, flag == true);
	work(mid, r, false);
	base_sort(l, r);
	if (flag){
		calc(id + l, mid - l, id + mid, r - mid);
	}
	if (flag){
		sort(id + mid, id + r, cmp);
		work(mid, r, flag == true);
	}
	base_sort(l, r);
	for(int i = l; i < r; i++){
		id[i] = tmp[i];
	}
}

void init(){
	for(int i = 0; i < m; i++){
		int x, y, z;
		v[i].read();
	}
	C = ~(1 << 31), M = (1 << 16) - 1;
	for(int i = m; i < m + n; i++){
		v[i].x = r();
		v[i].y = r();
		v[i].z = r();
	}
	n = m + n;
	for(int i = 0; i < n; i++){
		v[i].x += 1;
		v[i].y += 1;
		v[i].z += 1;
	}
	sort(v, v + n);
	for(int i = 0; i < n; i++){
		id[i] = i;
		ans[i] = 1;
	}
}

void solve(){
	work(0, n, true);
	int ret = 0;
	for(int i = 0; i < n; i++){
		ret = max(ret, ans[i]);
	}
	printf("%d\n", ret);
}

int main(){
	tot = 0;
	while(~scanf("%d%d%d%d", &m, &n, &a, &b)){
		if (!n && !m && !a && !b) break;
		init();
		solve();
	}
	return 0;
}