eolymp
bolt
Try our new interface for solving problems
Problems

Longest Chain

Longest Chain

Time limit 60 seconds
Memory limit 256 MiB

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 data

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 1m+n3×10^5, 1A, B2^16, and 0x_k, y_k, z_k < 10^6 for 1km+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 data

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.

Examples

Input example #1
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 example #1
3
5
1
3
Source ACM International Collegiate Programming Contest, Asia Regional Contest, Aizu, 2013–11–24