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 <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<int, P> Pint;
typedef pair<P, P> PP;
int a, b, C, M;
int r() {
  a = 36969 * (a & M) + (a >> 16);
  b = 18000 * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000;
}
vector<P> v1[300001];
vector<int> v[300001];
vector<Pint> p;
int ct;
void init(){
    for(int i=1; i<=ct; i++){
        v[i].clear(); v1[i].clear();
		vector<int>().swap(v[i]);
		vector<P>().swap(v1[i]);
    }
  for(int i=0; i<p.size(); i++){
    int x=p[i].first, y=p[i].second.first, z=p[i].second.second;
      int i0=y;
      while(i0<=ct){
        v1[i0].push_back(P(z, i));
        i0+=(i0&(-i0));
      }
  }
  for(int i=1; i<=ct; i++){
    if(v1[i].empty()) continue;
    sort(v1[i].begin(), v1[i].end());
    for(int j=0; j<=v1[i].size(); j++){
      v[i].push_back(0);
    }
  }
}
     
void update(int k, int i, int a){
   while(i<=v1[k].size()){
       v[k][i]=max(v[k][i], a);
       i+=(i&(-i));
   }
}
    
void update0(int i, int y, int z, int a){
    int k=y;
    while(k<=ct){
        int i0=lower_bound(v1[k].begin(), v1[k].end(), P(z, i))-v1[k].begin();
        update(k, i0+1, a);
        k+=(k&(-k));
    }
}
    
int find(int k, int i){
    int s=0;
    while(i>0){
        s=max(s, v[k][i]);
        i-=(i&(-i));
    }
    return s;
}
    
int find0(int y, int z){
    int s=0;
    int k=y-1;
    while(k>0){
        if(!v1[k].empty()){
            int i=lower_bound(v1[k].begin(), v1[k].end(), P(z, -1))-v1[k].begin();
            if(i!=0) s=max(s, find(k, i));
        }
        k-=(k&(-k));
    }
    return s;
}
    
int main()
{
    while(1){
      int m, n, A, B;
      scanf("%d %d %d %d", &m, &n, &A, &B);
      if(m==0 && n==0 && A==0 && B==0) return 0;
      a=A, b=B, C=~(1<<31), M=(1<<16)-1;
      for(int i=0; i<m; i++){
        int x, y, z;
        cin>>x>>y>>z;
        p.push_back(Pint(x, P(y, z)));
      }
      for(int i=0; i<n; i++){
        int x=r();
        int y=r();
        int z=r();
        p.push_back(Pint(x, P(y, z)));
      }
      sort(p.begin(), p.end());
      p.erase(unique(p.begin(), p.end()), p.end());
      int ind[300000];
      for(int i=0; i<p.size(); i++) ind[i]=i;
      sort(ind, ind+p.size(), [&](int x, int y){ return p[x].second.first<p[y].second.first;});
      int pr=-1;
      ct=0;
      for(int i=0; i<p.size(); i++){
          if(pr!=p[ind[i]].second.first){
              pr=p[ind[i]].second.first;
              ct++;
              p[ind[i]].second.first=ct;
          }else{
              p[ind[i]].second.first=ct;
          }
      }
      init();
      vector<PP> mxv;
      int xp=-1;
      for(int i=0; i<p.size(); i++){
        int x=p[i].first, y=p[i].second.first, z=p[i].second.second;
        if(xp==x){
          mxv.push_back(PP(P(i, y), P(z, find0(y, z))));
        }else{
          for(auto t:mxv){
            update0(t.first.first, t.first.second, t.second.first, t.second.second+1);
          }
          mxv.clear();
          xp=x;
          mxv.push_back(PP(P(i, y), P(z, find0(y, z))));
        }
      }
      for(auto t:mxv){
        update0(t.first.first, t.first.second, t.second.first, t.second.second+1);
      }
      printf("%d\n", find0(ct+1, 1000000));
      p.clear();
    }
    return 0;
}