#include <bits/stdc++.h>

using namespace std;

int m, n, A, B;
int a, b, C, M;
vector < pair< int, pair<int,int> > > q;
vector < set < pair<int,int> > > s;

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

int cmp(const pair< int,pair<int,int> > &a, const pair< int,pair<int,int> > &b)
{
	if (a.first<b.first) return 1;
	else if (a.first==b.first && a.second.first>b.second.first) return 1;
	else if (a.first==b.first && a.second.first==b.second.first && a.second.second>b.second.second) return 1;
	return 0;
}

int check(pair<int,int> x, set< pair<int,int> > &s)
{
	if (s.size()==0) return 1;
	set < pair<int,int> > :: iterator it = s.lower_bound(x);
	//cout<<"check : "<<it->first<<"--"<<it->second<<endl;
	if (it!=s.begin()) it--;
	else return 1;
	if (it!=s.begin() && it->first==x.first) it--;
	if (it->first<x.first && it->second<x.second) return 0;
	else return 1;
}

int main()
{
	while (scanf("%d%d%d%d", &m, &n, &A, &B)==4){
		if (!m && !n && !A && !B) break;
		q.clear();
		s.clear();
		for (int i = 1; i<=m; i++){
			int Ta, Tb, Tc;
			scanf("%d%d%d", &Ta, &Tb, &Tc);
			q.push_back(make_pair(Ta, make_pair(Tb,Tc)));
		}
		a = A, b = B, C = ~(1<<31), M = (1<<16)-1;
		for (int i = 1; i<=n; i++){
			int Ta, Tb, Tc;
			Ta = r(); Tb = r(); Tc = r();
			q.push_back(make_pair(Ta, make_pair(Tb,Tc)));
			//cout<<Ta<<"---"<<Tb<<"---"<<Tc<<endl;
		}
		sort(q.begin(), q.end(), cmp);
		for (int i = 0; i<q.size(); i++){
			//if (i!=0 && q[i].first==q[i-1].first) continue;
			pair <int,int> x = q[i].second;
			//cout<<q[i].first<<"---"<<x.first<<"---"<<x.second<<endl;
			int pos = -1;
			int l, r, mid;
			l = 0; r = s.size()-1;
			while (l<=r){
				mid = (l+r)/2;
				//cout<<"mid : "<<mid<<endl;
				if (check(x,s[mid])){
					pos = mid;
					r = mid-1;
				}
				else{
					l = mid+1;
				}
			}
			//cout<<pos<<endl;
			if (pos!=-1){
				set < pair<int,int> > :: iterator it = s[pos].lower_bound(x);
				if (it!=s[pos].begin()){
					it--;
					if (it->first==x.first || it->second==x.second) continue;
				}
				it = s[pos].upper_bound(x);
				while (it!=s[pos].end() && it->second>=x.second) s[pos].erase(it++);
				s[pos].insert(x);
			}
			else{
				set < pair<int,int> > Ts;
				Ts.insert(x);
				s.push_back(Ts);
			}	
		}
		cout<<s.size()<<endl;
	}
	return 0;
}
