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 <queue>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <map>
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 1000010
#define INF (1000000050)

typedef set< pair<int,int> >::iterator It;
int nextscore[MAX_N];
set< pair<int,int> > seg[MAX_N];

void add(int x,int y,int score){
	//cout << x << "," << y << "," << score << endl;
	while(x<MAX_N){
		It it1 = --seg[x].lower_bound(make_pair(y+1,-INF));
		It it2 = seg[x].lower_bound(make_pair(y+1,-INF));
		
		if( it1->first <= y && it1->second >= score ){
			x += x & -x;
			continue;
		}
		while( it2->first != INF && it2->second <= score ){
			seg[x].erase(it2++);
		}
		seg[x].insert(make_pair(y,score));
		x += x & -x;
	}
}
int get(int x,int y){
	//cout << "(" << x << "," << y << ")" << endl;
	int maximum = 0;
	while(x>0){
		It it = --seg[x].lower_bound(make_pair(y+1,-INF));
		
		
		/*
		It	 it = seg[x].begin();
		while( it != seg[x].end() ){
			if( it->first <= y ){
				maximum = max(maximum,it->second);
			}
			it++;
		}
		*/
		//cout << " (" << it->first << "," << it->second << ")" << endl;
		maximum = max(maximum,it->second);
		x -= x & -x;
	}
	return maximum;
}


int aa, bb, C = ~(1<<31), M = (1<<16)-1;

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

vector< pair<int, pair<int,int> >  > inp;

int main(){
	for(int i = 0 ; i < MAX_N ; i++){
		seg[i].insert(make_pair(INF,INF));
		seg[i].insert(make_pair(-INF,0));
	}
	int m,n;
	while(	cin >> m >> n >> aa >> bb && m+n ){
		inp.clear();
		for(int i = 0 ; i < MAX_N ; i++){
			if( seg[i].size() != 2 ){
				seg[i].clear();
				seg[i].insert(make_pair(INF,INF));
				seg[i].insert(make_pair(-INF,0));
			}
		}
		
		vector<int> ux;
		for(int i = 0 ; i < m ; i++){
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			a++;
			b++;
			c++;
			inp.push_back(make_pair(c,make_pair(a,b)));
		}
		for(int i = 0 ; i < n ; i++){
			int a,b,c;
			a=r()+1;
			b=r()+1;
			c=r()+1;
			inp.push_back(make_pair(c,make_pair(a,b)));
		}
		sort(inp.begin(),inp.end());
		int res = 0;
		for(int i = 0 ; i < inp.size() ; ){
			
			int j = i;
			while( j < inp.size() && inp[i].first == inp[j].first ) j++;
			for(int k = i ; k < j ; k++){
				int a = inp[k].second.first;
				int b = inp[k].second.second;
				nextscore[k] = get(a-1,b-1) + 1;
				res = max( res , nextscore[k] );
				//cout << k << ": " << inp[k].first << " " << inp[k].second.first << " " << inp[k].second.second << " " <<  get(a-1,b-1) <<  endl;
			}
			for(int k = i ; k < j ; k++){
				int a = inp[k].second.first;
				int b = inp[k].second.second;
				add(a,b,nextscore[k]);
			}
			i = j;
		}
		cout << res << endl;
		//return 0;
	}
	
}