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<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int a,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;
}

set<pair<int,int>> st[303030];
int main(){
    int m,n;
    while(cin>>m>>n>>a>>b,m+n!=0){
        int l=m+n;
        int x[l],y[l],z[l];
        rep(i,m)cin>>x[i]>>y[i]>>z[i];
        rep(i,n){
            x[i+m]=r();
            y[i+m]=r();
            z[i+m]=r();
        }
        vector<int> ord(l);
        rep(i,l)ord[i]=i;
        sort(ord.begin(),ord.end(),[&](int s,int t){return x[s]==x[t] ? y[s]>y[t] : x[s]<x[t];});
        rep(i,303030){
            if(st[i].size()==2)continue;
            st[i].clear();
            st[i].insert({inf,-inf});
            st[i].insert({-inf,inf});
        }
        int ans=0;
        for(auto i:ord){
            int left=0,right=ans+1;
            while(right-left>1){
                int mid=(left+right)/2;
                auto itr=--st[mid].lower_bound({y[i],-1});
                if(itr->second<z[i])left=mid;
                else right=mid;
            }
            int ret=left+1;
            ans=max(ret,ans);
            auto itr=--st[ret].lower_bound({y[i],inf});
            if(z[i]>=itr->second)continue;
            if(itr->first<y[i])itr++;
            while(itr->second>=z[i])itr=st[ret].erase(itr);
            st[ret].insert({y[i],z[i]});
        }
        cout<<ans<<endl;
    } 
    return 0;
}