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 <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <tuple>
#include <vector>
using namespace std;

typedef tuple<int, int, int> point;
#define x(p) get<0>(p)
#define y(p) get<1>(p)
#define z(p) get<2>(p)

inline int r(int &a, int &b) {
	static constexpr int C = ~(1 << 31);
	static constexpr int M = (1 << 16) - 1;

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

inline bool check(int y, int z, const map<int, int> &contour) {
	const auto it = contour.upper_bound(y);
	return it != contour.end() && it->second < z;
}

int solve(vector<point> &points) {
	sort(points.begin(), points.end());

	int max_count = 0;
	int prev_x = x(points.front());
	vector<tuple<int, int, int>> buf;
	vector<map<int, int>> maps{{{1, -1}}};
	buf.reserve(points.size());
	maps.reserve(points.size());

	for(const auto &p : points) {
		const int x = x(p);
		const int y = -y(p);
		const int z = z(p);

		if(prev_x != x) {
			maps.resize(max_count + 1);
			prev_x = x;

			for(const auto &e : buf) {
				int k, ty, tz;
				tie(k, ty, tz) = e;

				auto &target = maps[k];
				auto it = target.lower_bound(ty);

				if(it == target.end() || it->second > tz) {
					target[ty] = tz;
					it = target.find(ty);

					while(it != target.begin()) {
						--it;
						if(tz > it->second) break;
						it = target.erase(it);
					}
				}
			}
			buf.clear();
		}

		int low = 0;
		int high = maps.size();

		while(low + 1 < high) {
			const int mid = (low + high) / 2;
			if(check(y, z, maps[mid])) {
				low = mid;
			}
			else {
				high = mid;
			}
		}

		if(low == max_count) ++max_count;
		buf.emplace_back(high, y, z);
	}

	return max_count;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int m, n, a, b; cin >> m >> n >> a >> b && a;) {
		vector<point> points;
		points.reserve(n + m);

		for(int i = 0; i < m; ++i) {
			int x, y, z;
			cin >> x >> y >> z;
			points.emplace_back(x, y, z);
		}

		for(int i = 0; i < n; ++i) {
			const int x = r(a, b);
			const int y = r(a, b);
			const int z = r(a, b);
			points.emplace_back(x, y, z);
		}

		cout << solve(points) << endl;
	}

	return EXIT_SUCCESS;
}