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 sz(x) int((x).size())
#define mkt make_tuple
#define fr first
#define sc second
using namespace std;

typedef tuple< int, int, int > Data;
const int kSize = 300010;
const int kMax = 1 << 20;

int M, N, A, B;
Data T[kSize];

map< int, int > tree[kMax];
int mxy;

void read();
void solve();

inline int low_bit(int i) { return i & (-i); }

int query(int y, int z) {
  if (y == 0 || z == 0)
    return 0;

  int i = y, ret = 0;
  while (i > 0) {
    auto &tr = tree[i - 1];
    auto itr = tr.upper_bound(z - 1);
    if (itr != tr.begin())
      ret = max(ret, (--itr)->sc);
    i -= low_bit(i);
  }

  return ret;
}

void update(int y, int z, int v) {
  for (int i = y + 1; i < mxy + 10; i += low_bit(i)) {
    auto &tr = tree[i - 1];
    auto itr = tr.lower_bound(z), iitr = itr;
    bool is_ins = false;

    if (tr.count(z) > 0) {
      if (tr[z] < v)
        tr[z] = v, iitr = tr.find(z), is_ins = true;
    }
    else if (itr == tr.begin())
      tr[z] = v, iitr = tr.find(z), is_ins = true;
    else {
      if ((--itr)->sc < v)
        tr[z] = v, iitr = tr.find(z), is_ins = true;
    }

    if (is_ins) {
      ++iitr;
      while (iitr != tr.end()) {
        if (iitr->sc > v)
          break;
        auto tmp = iitr;
        ++iitr, tr.erase(tmp);
      }
    }
  }
}

int main() {
  while (scanf(" %d %d %d %d", &M, &N, &A, &B), M + N)
    read(), solve();

  return 0;
}

void read() {
  int x, y, z;
  int a = A, b = B, c = ~(1 << 31), m = (1 << 16) - 1;
  auto r = [&] () {
    a = 36969 * (a & m) + (a >> 16);
    b = 18000 * (b & m) + (b >> 16);
    return (c & ((a << 16) + b)) % 1000000;
  };

  mxy = 0;
  for (int i = 0; i < M; ++i) {
    scanf(" %d %d %d", &x, &y, &z), mxy = max(mxy, y);
    T[i] = mkt(x, y, z);
  }

  for (int i = 0; i < N; ++i) {
    x = r(), y = r(), z = r(), mxy = max(mxy, y);
    T[i + M] = mkt(x, y, z);
  }
}

void solve() {
  static int f[kSize];

  sort(&T[0], &T[M + N]);

  for (int i = 0; i < mxy + 10; ++i)
    tree[i].clear();

  fill(&f[0], &f[M + N], 0);
  for (int i = 0, j = 0; i < M + N; i = j) {
    j = i + 1;
    while (j < M + N && get< 0 >(T[i]) == get< 0 >(T[j]))
      ++j;

    for (int k = i; k < j; ++k) {
      int y = get< 1 >(T[k]), z = get< 2 >(T[k]);
      f[k] = query(y, z) + 1;
    }

    for (int k = i; k < j; ++k) {
      int y = get< 1 >(T[k]), z = get< 2 >(T[k]);
      update(y, z, f[k]);
    }
  }

  cout << *max_element(&f[0], &f[M + N]) << "\n";
}