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;


#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int M,N,A,B;

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;
}

map<int,int>lis[333333];

void solve(){
    vector<tuple<int,int,int>>ps;
    rep(i,M){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        ps.pb(make_tuple(x,y,z));
    }
    a=A;b=B;
    rep(i,N){
        int x,y,z;
        x=r();y=r();z=r();
        ps.pb(make_tuple(x,y,z));
    }

    N+=M;
    rep(i,N)get<1>(ps[i])*=-1;
    sort(all(ps));
    rep(i,N)get<1>(ps[i])*=-1;

    rep(i,N+1)lis[i].clear();

    /*
    vector<int>latte;
    rep(i,N){
        latte.pb(get<0>(ps[i]));
        latte.pb(get<1>(ps[i]));
        latte.pb(get<2>(ps[i]));
    }
    sort(all(latte));latte.erase(unique(all(latte)),latte.end());
    rep(i,N){
        get<0>(ps[i])=lower_bound(all(latte),get<0>(ps[i]))-latte.begin();
        get<1>(ps[i])=lower_bound(all(latte),get<1>(ps[i]))-latte.begin();
        get<2>(ps[i])=lower_bound(all(latte),get<2>(ps[i]))-latte.begin();
    }
    rep(i,N){
        cout<<get<0>(ps[i])<<" "<<get<1>(ps[i])<<" "<<get<2>(ps[i])<<endl;
    }*/

    for(auto &w:ps){
        int x=get<1>(w),y=get<2>(w);
        //cout<<" in "<<x<<" "<<y<<endl;
        int lb=0,ub=N;
        while(ub-lb>1){
            int mid=(ub+lb)/2;
            if([&](){
                auto it=lis[mid].lower_bound(x);
                if(it==lis[mid].begin())return false;
                it--;
                return it->se<y;
            }())lb=mid;
            else ub=mid;
        }

        if(lis[ub].find(x)!=lis[ub].end())chmin(lis[ub][x],y);
        else lis[ub][x]=y;

        while(true){
            auto it=lis[ub].upper_bound(x);
            if(it==lis[ub].end())break;
            if(it->se<=y)break;
            lis[ub].erase(it);
        }
        /*
        for(int j=0;j<=N;j++){
            if(lis[j].size()==0)continue;
            cout<<j<<"-------"<<endl;
            for(auto &z:lis[j])cout<<"("<<z.fi<<","<<z.se<<")"<<" ";cout<<endl;
        }*/
    }

    int ans=0;
    rep(i,N+1)if(lis[i].size())ans=i;
    printf("%d\n",ans);
}
signed main(){
    while(scanf("%d%d%d%d",&M,&N,&A,&B),M||N||A||B)solve();
    return 0;
}