Problem:
Problem G: Longest Chain

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.

Sample 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 for the Sample Input

3
5
1
3

Solution in C++14 (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto&(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                          \
    cout << #a << ":";                     \
    rep(kbrni, n) cout << " " << a[kbrni]; \
    cout << endl
#define svec(v)                                   \
    cout << #v << ":";                            \
    rep(kbrni, v.size()) cout << " " << v[kbrni]; \
    cout << endl
#define svecp(v)                                                              \
    cout << #v << ":";                                                        \
    each(kbrni, v) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \
    cout << endl
#define sset(s)                          \
    cout << #s << ":";                   \
    each(kbrni, s) cout << " " << kbrni; \
    cout << endl
#define smap(m)                                                               \
    cout << #m << ":";                                                        \
    each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \
    cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

int A, 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;
}

set<P> st[300005];
vector<P> v[1000000];

bool contain(int cri, P& nw)
{
    auto it = st[cri].lower_bound(P(nw.fi - 1, nw.se));
    if (it != st[cri].begin()) {
        --it;
        return it->se < nw.se;
    } else {
        return false;
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (1) {
        int n, m;
        cin >> m >> n >> A >> B;
        rep(i, n + m + 1)
        {
            st[i].clear();
        }
        rep(i, 1000000)
        {
            v[i].clear();
        }
        if (n == 0 && m == 0 && A == 0 && B == 0) {
            break;
        }
        rep(i, m)
        {
            int s, t, u;
            cin >> s >> t >> u;
            v[s].pb(P(t, u));
        }
        rep(i, n)
        {
            v[r()].pb(P(r(), r()));
        }
        int mx = -1;
        rep(i, 1000000)
        {
            if (len(v[i])) {
                sort(all(v[i]), [&](const P& p, const P& q) {
                    return (p.fi == q.fi) ? (p.se > q.se) : (p.fi > q.fi);
                });
                each(it, v[i])
                {
                    int l = -1, r = m + n + 1;
                    while (r - l > 1) {
                        int mid = (l + r) / 2;
                        if (contain(mid, it)) {
                            l = mid;
                        } else {
                            r = mid;
                        }
                    }
                    auto it2 = st[r].upper_bound(it);
                    if (it2 != st[r].begin()) {
                        --it2;
                        if (it2->se == it.se or it2->fi == it.fi) {
                            continue;
                        }
                        ++it2;
                    }
                    while (1) {
                        if (it2 != st[r].end()) {
                            if (it2->se >= it.se) {
                                it2 = st[r].erase(it2);
                            } else {
                                cmx(mx, r);
                                st[r].insert(it);
                                break;
                            }
                        } else {
                            cmx(mx, r);
                            st[r].insert(it);
                            break;
                        }
                    }
                }
            }
        }
        cout << mx + 1 << "\n";
    }
    return 0;
}