Longest Chain
Let us compare two triples a = (x_a, y_a, z_a) and b = (x_b, y_b, z_b) by a partial order ≺ defined as follows.
a ≺ b x_a < x_b and y_a < y_b and z_a < z_b
Your mission is to find, in the given set of triples, the longest ascending series a_1 ≺ a_2 ≺ ... ≺ a_k.
Input
The input is a sequence of datasets, each specifying a set of triples formatted as follows.
m n A B
x_1 y_1 z_1
x_2 y_2 z_2
...
x_m y_m z_m
Here, m, n, A and B in the first line, and all of x_i, y_i and z_i (i = 1, ..., m) in the following lines are non-negative integers.
Each dataset specifies a set of m+n triples. The triples p_1 through p_m are explicitly specified in the dataset, the i-th triple p_i being (x_i, y_i, z_i). 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 x_{m+1}, y_{m+1}, z_{m+1}, x_{m+2}, y_{m+2}, z_{m+2}, ..., x_{m+n}, y_{m+n}, and z_{m+n}, in this order.
You can assume that 1 ≤ m+n ≤ 3×10^5, 1 ≤ A, B ≤ 2^16, and 0 ≤ x_k, y_k, z_k < 10^6 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×10^6.
Output
For each dataset, output the length of the longest ascending series of triples in the specified set.
If p_i1 ≺ p_i2 ≺ ... ≺ p_ik is the longest, the answer should be k.