#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
int n;
map<int,int> M[300005];
int A,B,C,MM;
inline int r() {
	A = 36969 * (A & MM) + (A >> 16);
    B = 18000 * (B & MM) + (B >> 16);
    return (C & ((A << 16) + B)) % 1000000;
}
struct Tuple {
	int x,y,z;
	void read() {
		scanf("%d%d%d", &x,&y,&z);
	}
	void getinput() {
		x=r(), y=r(), z=r();
	}
}P[300005];
bool cmp_x(const Tuple &a, const Tuple &b) {
	return a.x < b.x;
}
inline bool okok(int index,int qy,int qz) {
	auto &c=M[index];
	auto f=c.lower_bound(qy);	
	if(f!=c.begin() && (--f)->second<qz)
		return 1;	
	return 0;
}
int ask(int qy,int qz) {
	int l=0,r=n;	
	while(l<r) {
		int mid=(l+r+1)>>1;		
		if(okok(mid,qy,qz))	l=mid;
		else				r=mid-1;
	}	
	return l;
}
void update(int dpv,int qy,int qz) {
	auto &c=M[dpv];
	auto f=c.upper_bound(qy);
	if(f!=c.begin() && (--f)->second<=qz)		return;
	f=c.lower_bound(qy);
	while(f!=c.end() && f->second>=qz)		c.erase(f++);
	c[qy]=qz;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    // freopen("F.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    // ios::sync_with_stdio(false);		cin.tie(0);                  
    int m;
    while(scanf("%d%d%d%d", &m,&n,&A,&B)==4) {
    	if(!m && !n && !A && !B)	break;
    	C=~(1U<<31), MM=(1<<16)-1;
    	rep(i,m)		P[i].read();
    	REP(i,m,m+n)	P[i].getinput();
    	n+=m;    	
    	sort(P,P+n,cmp_x);    	
    	int res=0;
    	FOR(i,0,n)	M[i].clear();
    	static int tmp[300005];
    	for(int i=0,j;i<n;) {
    		j=i;    		
    		for(;j<n && P[j].x==P[i].x;++j) {
    			res=max(res, tmp[j]=ask(P[j].y,P[j].z)+1);    			
    		}
    		for(;i<j;++i)
    			update(tmp[i],P[i].y,P[i].z);
    	}
    	printf("%d\n",res);
    }
    return 0;
}