#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;
}


Measured memory_bytes: 60320