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 <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <limits>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
constexpr int INF = 1 << 28;
inline bool check(const set<P>& st, const P& v)
{
    auto upper = st.upper_bound({v.first, -INF});
    if (upper == st.begin()) {
        return false;
    } else {
        upper--;
        return upper->second < v.second;
    }
}
inline bool shouldInsert(set<P>& st, const P& v)
{
    auto upper = st.upper_bound(v);
    if (upper == st.begin()) {
        return true;
    } else {
        upper--;
        return upper->first != v.first and upper->second != v.second;
    }
}
inline void insertAndErase(set<P>& st, const P& v)
{
    auto lower = st.lower_bound({v.first, -INF});
    for (; lower != st.end() and lower->second >= v.second;) {
        lower = st.erase(lower);
    }
    if (shouldInsert(st, v)) {
        st.insert(v);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true) {
        int M, N, A, B;
        cin >> M >> N >> A >> B;
        if (M == 0 and N == 0 and A == 0 and B == 0) {
            break;
        }
        int a = A;
        int b = B;
        const auto gen = [&]() {
            constexpr int C = ~(1 << 31);
            constexpr int M = (1 << 16) - 1;
            a = 36969 * (a & M) + (a >> 16);
            b = 18000 * (b & M) + (b >> 16);
            return (C & ((a << 16) + b)) % 1000000;
        };
        const auto comp = [](const PP& pp1, const PP& pp2) {
            return pp1.first != pp2.first ? pp1.first < pp2.first : pp1.second > pp2.second;
        };
        vector<PP> p(M + N);
        for (int i = 0; i < M; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            p[i] = {x, {y, z}};
        }
        for (int i = M; i < M + N; i++) {
            const int x = gen();
            const int y = gen();
            const int z = gen();
            p[i] = {x, {y, z}};
        }
        sort(p.begin(), p.end(), comp);
        vector<set<P>> tail(M + N + 1);
        tail[0].insert({-INF, -INF});
        for (int i = 0; i < M + N; i++) {
            int inf = -1;
            int sup = N + M + 1;
            while (sup - inf > 1) {
                const int mid = (sup + inf) / 2;
                if (check(tail[mid], p[i].second)) {
                    inf = mid;
                } else {
                    sup = mid;
                }
            }
            insertAndErase(tail[sup], p[i].second);
        }
        for (int i = N + M; i >= 0; i--) {
            if (not tail[i].empty()) {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}