Longest Chain

No attempts yetTime limit10sMemory limit128 MB

Problem

Define a partial order \prec on triples of integers a=(xa,ya,za)a = (x_a, y_a, z_a) and b=(xb,yb,zb)b = (x_b, y_b, z_b) as follows.

abxa<xb, ya<yb, za<zba \prec b \quad\Longleftrightarrow\quad x_a < x_b,\ y_a < y_b,\ z_a < z_b

All three coordinates have to grow for aba \prec b to hold.

You are given a set of triples. Find the longest ascending series a1a2aka_1 \prec a_2 \prec \cdots \prec a_k in it.

Input

The input is a sequence of datasets. One dataset has this format.

m n A B
x1 y1 z1
x2 y2 z2
...
xm ym zm

The values mm, nn, AA, BB on the first line and every xix_i, yiy_i, ziz_i on the following mm lines are non-negative integers.

One dataset specifies m+nm + n triples. The triples p1p_1 through pmp_m are written out in the dataset, and the ii-th triple pip_i is (xi,yi,zi)(x_i, y_i, z_i). The remaining nn triples come from the generator below, started with the parameters AA and BB.

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

Every operation in this code runs on 32-bit signed integers. Multiplication and addition wrap around in two's complement once the result leaves that range, and >> is an arithmetic shift that keeps the sign.

Calling r() 3n3n times in a row yields xm+1x_{m+1}, ym+1y_{m+1}, zm+1z_{m+1}, xm+2x_{m+2}, ym+2y_{m+2}, zm+2z_{m+2}, \ldots, xm+nx_{m+n}, ym+ny_{m+n}, zm+nz_{m+n}, in this order.

You can assume that 1m+n3×1051 \le m + n \le 3 \times 10^5 and 1A,B2161 \le A, B \le 2^{16}, and that 0xk,yk,zk<1060 \le x_k, y_k, z_k < 10^6 for every kk with 1km+n1 \le k \le m + n.

The input ends with a line containing four zeros. The total of m+nm + n over all datasets does not exceed 2×1062 \times 10^6.

Output

For each dataset, print the length of the longest ascending series of triples, one length per line. If pi1pi2pikp_{i_1} \prec p_{i_2} \prec \cdots \prec p_{i_k} is the longest, the answer is kk.