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<map>
#include<set>
#include<queue>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<algorithm>
typedef long long LL;
#define MAXN 300010
int A, B, C = ~(1<<31), M = (1<<16)-1;
int myRand() {
  A = 36969 * (A & M) + (A >> 16);
  B = 18000 * (B & M) + (B >> 16);
  return (C & ((A << 16) + B)) % 1000000;
}
int N, zn, z[MAXN], f[MAXN], max[MAXN];
struct V {
	int x, y, z;
	bool operator < (const V &t) const {
		if(x != t.x) {
			return x < t.x;
		}
		if(y != t.y) {
			return y > t.y;
		}
		return z > t.z;
	}
}v[MAXN];
struct T {
	int id, x, y, z;
	bool operator < (const T &t) const {
		if(y != t.y) {
			return y < t.y;
		}
		if(x != t.x) {
			return x > t.x;
		}
		return z > t.z;
	}
}t[MAXN];
int query(int x) {
	int ans = 0;
	for(int i = x; i > 0; i -= i & -i) {
		ans = std::max(ans, max[i]);
	}
	return ans;
}
void update(int x, int v) {
	for(int i = x; i <= zn; i += i & -i) {
		max[i] = std::max(max[i], v);
	}
}
void clear(int x) {
	for(int i = x; i <= zn; i += i & -i) {
		max[i] = 0;
	}
}
void cdq(int x, int y) {
	if(x == y) {
		return ;
	}
	int mid = (x + y) >> 1;
	cdq(x, mid);
	for(int i = x; i <= y; i ++) {
		t[i].id = i;
		t[i].x = v[i].x, t[i].y = v[i].y, t[i].z = v[i].z;
	}
	std::sort(t + x, t + y + 1);
	for(int i = x; i <= y; i ++) {
		if(t[i].id <= mid) {
			update(t[i].z, f[t[i].id]);
		} else {
			f[t[i].id] = std::max(f[t[i].id], query(t[i].z - 1) + 1);
		}
	}
	for(int i = x; i <= y; i ++) {
		if(t[i].id <= mid) {
			clear(t[i].z);
		}
	}
	cdq(mid + 1, y);
}
int main() {
	//freopen("test.in", "rb", stdin);
	int m;
	while(scanf("%d%d%d%d", &N, &m, &A, &B), N + m > 0) {
		for(int i = 0; i < N; i ++) {
			scanf("%d%d%d", &v[i].x, &v[i].y, &v[i].z);
		}
		for(int i = 0; i < m; i ++) {
			v[N].x = myRand();
			v[N].y = myRand();
			v[N].z = myRand();
			++ N;
		}
		std::sort(v, v + N);
		for(int i = 0; i < N; i ++) {
			z[i] = v[i].z;
		}
		std::sort(z, z + N);
		zn = std::unique(z, z + N) - z;
		for(int i = 0; i < N; i ++) {
			v[i].z = std::lower_bound(z, z + zn, v[i].z) - z + 1;
		}
		for(int i = 0; i < N; i ++) {
			f[i] = 1;
		}
		cdq(0, N - 1);
		int ans = 0;
		for(int i = 0; i < N; i ++) {
			ans = std::max(ans, f[i]);
		}
		printf("%d\n", ans);
	}
	return 0;
}