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<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef complex<ld> Point;
const ll mod = 1000000007;
const ld INF = 1e+30;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)

//3D BIT
//[x,∞)×[y,∞)×[z,∞)に含まれる点の個数を数える
struct BIT {
private:
	vector<int> node; int n;
public:
	int f(int a, int b) {
		return max(a, b);
	}
	void init(int n_) {
		n = n_; node.resize(n, 0);
	}
	//[0,a)
	int query(int a) {
		int ret = 0;
		for (int i = a - 1; i >= 0; i = (i&(i + 1)) - 1)ret = f(ret, node[i]);
		return ret;
	}
	void update(int a, int b) {
		for (int i = a; i < n; i |= i + 1)node[i] = f(node[i], b);
	}
};
struct BIT2 {
private:
	vector<BIT> node; int sz;
	vector<vector<int>> vy;
public:
	int f(int a, int b) {
		return max(a, b);
	}
	void init(int n) {
		sz = n; node.resize(n);
		vy.resize(n);
	}
	//先読み。
	void add(int x, int y) {
		for (int i = x; i < sz; i |= i + 1) {
			vy[i].push_back(y);
		}
	}
	//先読みの反映。
	void complete() {
		rep(i, sz) {
			sort(vy[i].begin(), vy[i].end());
			vector<int> cop = vy[i];
			vy[i].erase(unique(vy[i].begin(), vy[i].end()), vy[i].end());
			int siz = vy[i].size();
			node[i].init(siz);
		}
	}
	int query(int x, int y) {
		int ret = 0;
		for (int i = x - 1; i >= 0; i = (i&(i + 1)) - 1) {
			int idy = lower_bound(vy[i].begin(), vy[i].end(), y) - vy[i].begin();
			ret =f(ret, node[i].query(idy));
		}
		return ret;
	}
	void update(int x, int y,int k) {
		for (int i = x; i < sz; i |= i + 1) {
			int idy = lower_bound(vy[i].begin(), vy[i].end(), y) - vy[i].begin();
			node[i].update(idy, k);
		}
	}
};

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

struct triple {
	int x, y, z;
	bool operator<(const triple &t)const {
		if (x != t.x)return x < t.x;
		if (y != t.y)return y > t.y;
		return z > t.z;
	}
};
void solve() {
	int n, m;
	while (cin >> m >> n >> A >> B, A) {
		a = A, b = B, c = ~(1 << 31), M = (1 << 16) - 1;
		vector<triple> v;
		vector<int> vy;
		rep(i, m) {
			int x, y, z; cin >> x >> y >> z;
			v.push_back({ x,y,z });
			vy.push_back(y);
		}
		rep(i, n) {
			int x = r(); int  y = r();int z = r();
			v.push_back({ x,y,z });
			vy.push_back(y);
		}
		sort(vy.begin(), vy.end());
		vy.erase(unique(vy.begin(), vy.end()), vy.end());
		int len = vy.size();
		sort(v.begin(), v.end());
		int ans = 0;
		BIT2 b; b.init(len);
		rep(i, v.size()) {
			int idy = lower_bound(vy.begin(), vy.end(), v[i].y) - vy.begin();
			b.add(idy, v[i].z);
		}
		b.complete();
		rep(i, v.size()) {
			int idy = lower_bound(vy.begin(), vy.end(), v[i].y) - vy.begin();
			int z = b.query(idy, v[i].z); z++;
			ans = max(ans, z);
			b.update(idy, v[i].z, z);
			//cout << z << endl;
		}
		cout << ans << endl;
	}
}
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
	//stop
	return 0;
}