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 <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int,pii> tii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

const int nmax=300010;
set<pii> LIS[nmax];
vector<tii> ary;

void ins(int pos,pii p){
	auto it=LIS[pos].lower_bound(p);
	auto it2=it;it2--;
	if(it2->second<=p.second) return;
	while(p.second<=it->second) LIS[pos].erase(it++);
	LIS[pos].insert(p);
}

bool mini(int pos,pii p){
	auto it=LIS[pos].lower_bound(p);
	while(it->second<p.second){
		if(it->first<p.first) return true;
		it--;
	}
	return false;
}


inline int rng(int &a,int &b){
	const int C = ~(1<<31), M = (1<<16)-1;
	a=36969*(a & M)+(a >> 16);
	b=18000*(b & M)+(b >> 16);
	return (C&((a<<16)+b))%1000000;
}

int main(void){
	int n,m,A,B;
	while(cin >> n >> m >> A >> B){
		if(n==0&&m==0) break;
		rep(i,nmax){
			LIS[i].clear();
			LIS[i].insert(mp(inf,-1));
			LIS[i].insert(mp(-1,inf));
		}
		ary.clear();
		rep(i,n){
			int x,y,z;
			cin >> x >> y >> z;
			ary.pb(mp(-z,mp(y,x)));
		}
		rep(i,m){
			int x=rng(A,B),y=rng(A,B),z=rng(A,B);
			ary.pb(mp(-z,mp(y,x)));
		}
		sort(ary.rbegin(),ary.rend());
		int ans=0;
		rep(i,n+m){
			int l=-1,r=ans;
			while(r-l>1){
				int mid=(l+r)/2;
				if(mini(mid,ary[i].second))
					l=mid;
				else
					r=mid;
			}
			ans=max(ans,r+1);ins(r,ary[i].second);
		}
		cout << ans << endl;
	}
	return 0;
}