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 <cstdio>
#include <map>
#include <algorithm>
using namespace std;
 
int n;
 
struct tup{
    int x, y, z;
}a[300005];
 
map<int,int> mp[300005];
int dp[300005];
 
bool input(){
    int m, nn, A, B;
    scanf("%d %d %d %d",&m,&nn,&A,&B);
    if(m + nn + A + B == 0) return 0;
    n = m + nn;
    for(int i=1; i<=m; i++){
        scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].z);
    }
    int a = A, b = B, C = ~(1<<31), M = (1<<16)-1;
    for(int i=m+1; i<=n; i++){
      a = 36969 * (a & M) + (a >> 16);
      b = 18000 * (b & M) + (b >> 16);
      ::a[i].x = (C & ((a << 16) + b)) % 1000000;
      a = 36969 * (a & M) + (a >> 16);
      b = 18000 * (b & M) + (b >> 16);
      ::a[i].y = (C & ((a << 16) + b)) % 1000000;
      a = 36969 * (a & M) + (a >> 16);
      b = 18000 * (b & M) + (b >> 16);
      ::a[i].z = (C & ((a << 16) + b)) % 1000000;
    }
    for(int i=0; i<=n; i++){
        mp[i].clear();
    }
    return 1;
}
 
bool trial(int m, int y, int z){
    auto it = mp[m].lower_bound(y);
    if(it == mp[m].begin()) return 0;
    it--;
    return (z > it->second);
}
 

void add(int x, int y, int z){
	auto it = mp[x].lower_bound(y);
	auto it2 = mp[x].upper_bound(y);
	if(it2 != mp[x].begin()){
		it2--;
		if(it2->second <= z) return;
	}
	while(it != mp[x].end() && it->second >= z){
		mp[x].erase(it);
		it = mp[x].lower_bound(y);
	}
	mp[x].insert(make_pair(y, z));
}

void solve(){
    sort(a + 1, a + n + 1, [&](const tup &a, const tup &b){
        return a.x < b.x;
    });
    mp[0].insert(make_pair(-1, -1));
    for(int i=1; i<=n;){
        int e = i;
        while(e <= n && a[e].x == a[i].x) e++;
        for(int j=i; j<e; j++){
            int s = 0, e = i;
            while(s != e){
                int m = (s+e+1) / 2;
                if(trial(m, a[j].y, a[j].z)) s = m;
                else e = m-1;
            }
            dp[j] = s + 1;
        }
        for(int j=i; j<e; j++){
            add(dp[j], a[j].y, a[j].z);
        }
        i = e;
    }
    printf("%d\n",*max_element(dp+1,dp+n+1));
}
 
int main(){
    while(input()){
        solve();
    }
}