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>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

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

int main() {
  while(1){
    int m,n,A,B;
    cin>>m>>n>>A>>B;
    if(!(m+n))break;
    a=A;
    b=B;
    C = ~(1<<31);
    M = (1<<16)-1;
    vector<tuple<int,int,int>> v;
    REP(i,m){
      int x,y,z;
      cin>>x>>y>>z;
      v.emplace_back(x,y,z);
    }
    REP(i,n){
      int x=r(),y=r(),z=r();
      v.emplace_back(x,y,z);
    }
    sort(begin(v),end(v));
    vector<set<tuple<int,int>>> dp(1<<20);
    dp[0].insert(make_tuple(-1,-1));
    auto vitr = begin(v), veitr = begin(v);
    while(veitr != end(v)) {
      vector<tuple<int,int,int>> add;
      vector<tuple<int,int,int>> er;
      while(veitr != end(v)
          && get<0>(*veitr) == get<0>(*vitr)) ++veitr;
      for(; vitr != veitr; ++vitr) {
        int y,z;
        tie(ignore,y,z) = *vitr;
        int k=1<<19;
        int j=0;
        while(k){
          int nj=j+k;
          auto itr = dp[nj].lower_bound(make_tuple(y,0));
          if (itr != begin(dp[nj])) {
            --itr;
            if (get<1>(*itr) < z) j=nj;
          }
          k /= 2;
        }
        ++j;
        auto itr = dp[j].lower_bound(make_tuple(y,z));
        if (itr != begin(dp[j])) {
          int py,pz;
          tie(py, pz) = *prev(itr);
          if (py == y || pz == z) continue;
        }
        while(itr != end(dp[j])
            && (get<0>(*itr) == y || get<1>(*itr) >= z)) {
          er.emplace_back(j,get<0>(*itr),get<1>(*itr));
          ++itr;
        }
        add.emplace_back(j,y,z);
      }
      sort(begin(er),end(er));
      er.erase(unique(begin(er),end(er)),end(er));
      for (auto s:er) {
        int j,y,z;
        tie(j,y,z) = s;
        dp[j].erase(make_tuple(y,z));
      }
      sort(begin(add),end(add));
      add.erase(unique(begin(add),end(add)),end(add));
      auto itr = begin(add), eitr = begin(add);
      while(eitr != end(add)) {
        while(eitr != end(add) &&
            get<0>(*eitr) == get<0>(*itr)) ++eitr;
        vector<tuple<int,int>> tp;
        int j = get<0>(*itr);
        for(; itr != eitr; ++itr) {
          int y,z;
          tie(ignore,y,z) = *itr;
          //cout<<j<<' '<<y<<' '<<z<<endl;
          if (tp.empty() || (get<0>(tp.back()) < y && get<1>(tp.back()) > z)) {
            tp.emplace_back(y,z);
          }
        }
        for(auto r:tp) {
          dp[j].insert(r);
        }
      }
    }
    int maxl = 0;
    REP(i,1<<20) {
      if(!dp[i].empty()) {
        maxl = i;
      }
    }
    cout<<maxl<<endl;
  }
  return 0;
}