Define a partial order ≺ on triples of integers a=(xa,ya,za) and b=(xb,yb,zb) as follows.
a≺b⟺xa<xb, ya<yb, za<zb
All three coordinates have to grow for a≺b to hold.
You are given a set of triples. Find the longest ascending series a1≺a2≺⋯≺ak in it.
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 m, n, A, B on the first line and every xi, yi, zi on the following m lines are non-negative integers.
One dataset specifies m+n triples. The triples p1 through pm are written out in the dataset, and the i-th triple pi is (xi,yi,zi). The remaining n triples come from the generator below, started with the parameters A and B.
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() 3n times in a row yields xm+1, ym+1, zm+1, xm+2, ym+2, zm+2, …, xm+n, ym+n, zm+n, in this order.
You can assume that 1≤m+n≤3×105 and 1≤A,B≤216, and that 0≤xk,yk,zk<106 for every k with 1≤k≤m+n.
The input ends with a line containing four zeros. The total of m+n over all datasets does not exceed 2×106.
For each dataset, print the length of the longest ascending series of triples, one length per line. If pi1≺pi2≺⋯≺pik is the longest, the answer is k.