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 <vector>
#include <set>
#include <algorithm>
using namespace std;


const int inf = (int)1e8;


struct plane {

	set<pair<int, int> > xs, ys;

	void init()
	{
		xs.insert({-inf, +inf});
		ys.insert({+inf, -inf});

		xs.insert({+inf, -inf});
		ys.insert({-inf, +inf});
	}

	void print_all()
	{
		printf("pt:");
		for(auto pt : xs)
			printf(" (%d, %d)", pt.first, pt.second);
		printf("\n");
	}

	void add_point(int x, int y)
	{
		// assert is_conflicted(x, y) == false;

		auto l_pt1 = xs.lower_bound({x, y});
		auto r_pt2 = ys.lower_bound({y, x});
		auto r_pt1 = xs.lower_bound({r_pt2->second, r_pt2->first});
		auto l_pt2 = ys.lower_bound({l_pt1->second, l_pt1->first});
		bool can_put = false;
		bool can_del = false;

		if(*l_pt1 <= *r_pt1) {
			can_put = true;
			can_del = true;
		} else {
			auto r_next_pt1 = r_pt1;
			++r_next_pt1;
			if(l_pt1 == r_next_pt1 && x < l_pt1->first && y < r_pt1->second)
				can_put = true;
		}

		if(can_del) {
			xs.erase(l_pt1, ++r_pt1);
			ys.erase(r_pt2, ++l_pt2);
		}

		if(can_put) {
			xs.insert({x, y});
			ys.insert({y, x});
		}
	}

	bool is_conflicted(int x, int y)
	{
		auto r_next = xs.lower_bound({x, -inf});
		auto r = --r_next;
		return r->second < y;
	}

	int size()
	{
		// assert xs.size() == ys.size();
		return xs.size();
	}
};

struct point3 {
	int x, y, z;
	bool operator<(const point3 &rhs) const
	{
		if(x != rhs.x)
			return x < rhs.x;
		if(y != rhs.y)
			return y > rhs.y;
		return z > rhs.z;
	}
};

int generate_rand(int A, int B, bool init) {
	static int a, b;
	if(init) {
		a = A;
		b = B;
	}
	const int C = ~(1<<31), M = (1<<16)-1;
	a = 36969 * (a & M) + (a >> 16);
	b = 18000 * (b & M) + (b >> 16);
	return (C & ((a << 16) + b)) % 1000000;
}


int main()
{
	while(true) {

		int m, n, a, b;
		vector<point3> pts;
		vector<plane> box;

		scanf("%d%d%d%d", &m, &n, &a, &b);
		if(n + m == 0)
			break;
		for(int i = 0; i < m; ++i) {
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			pts.push_back({x, y, z});
		}
		for(int i = 0; i < n; ++i) {
			int x = generate_rand(a, b, i == 0);
			int y = generate_rand(a, b, false);
			int z = generate_rand(a, b, false);
			pts.push_back({x, y, z});
		}
		sort(pts.begin(), pts.end());

		box.push_back(plane());
		box[0].init();
		box[0].add_point(-1, -1);

		for(auto pt : pts) {

			//printf("[%d %d %d]\n", pt.x, pt.y, pt.z);

			if(box[box.size() - 1].is_conflicted(pt.y, pt.z)) {
				box.push_back(plane());
				box[box.size() - 1].init();
				box[box.size() - 1].add_point(pt.y, pt.z);
				//printf("added: %d(new)\n", box.size() - 1);
				//box[box.size() - 1].print_all();
				continue;
			}

			int lb = 0, ub = box.size() - 1;

			while(ub - lb > 1) {

				int mid = (lb + ub) / 2;

				//printf("check: %d\n", mid);
				//box[mid].print_all();

				if(box[mid].is_conflicted(pt.y, pt.z))
					lb = mid;
				else
					ub = mid;
			}

			box[ub].add_point(pt.y, pt.z);
			//printf("added: %d\n", ub);
			//box[ub].print_all();
		}

		int ans = box.size() - 1;

		printf("%d\n", ans);
	}

	return 0;
}