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:
/*
*   Copyright (C) 2015 All rights reserved.
*   
*   filename: 1341.cpp
*   author: doublehh
*   e-mail: sserdoublehh@foxmail.com
*   create time: 2015-03-11
*   last modified: 2015-03-11 23:11:23
*/
#include<bits/stdc++.h>
using namespace std;

const int maxn = 4e5;
struct Point
{
	int x, y, z, f;
};
int n, m;
Point pt[maxn];
int c[maxn];
int z[maxn];

bool cmpxY(const Point &p1, const Point &p2)
{ return p1.x < p2.x || (p1.x == p2.x && p1.y > p2.y); }
bool cmpyID(int a, int b)
{ return pt[a].y < pt[b].y || (pt[a].y == pt[b].y && a > b); }

#define lowbit(p) (p&-p)
void update(int p, int x)
{
	for (; p <= m; p += lowbit(p))
		c[p] = max(c[p], x);
}
int query(int p)
{
	int ret = 0;
	for (; p; p -= lowbit(p))
		ret = max(ret, c[p]);
	return ret;
}
void cls(int p)
{
	for (; p <= m; p += lowbit(p))
		c[p] = 0;
}

int id[maxn];
void rec(int l, int r)
{
	if (r - l == 1)
		return;

	int m = (l + r) >> 1;
	rec(l, m);

	for (int i = l; i < r; i++)
		id[i] = i;
	sort(id + l, id + r, cmpyID);

	for (int i = l; i < r; i++)
	{
		if (id[i] < m) update(pt[id[i]].z, pt[id[i]].f);
		else pt[id[i]].f = max(pt[id[i]].f, query(pt[id[i]].z - 1) + 1);
	}
	for (int i = l; i < m; i++) cls(pt[i].z);

	rec(m, r);
}
int a, b, C = ~(1<<31);
int M = (1<<16) - 1;
int r()
{
	a = 36969 * (a & M) + (a >> 16);
	b = 18000 * (b & M) + (b >> 16);
	return (C & ((a << 16) + b)) % 1000000;
}

void init()
{
	n += m;
	for (int i = 0; i < m; i++)
		scanf("%d %d %d", &pt[i].x, &pt[i].y, &pt[i].z);
	for (int i = m; i < n; i++)
		pt[i].x = r(), pt[i].y = r(), pt[i].z = r();
}

void solve()
{
	for (int i = 0; i < n; i++)
	{
		pt[i].f = 1;
		z[i] = pt[i].z;
	}

	sort(pt, pt + n, cmpxY);
	sort(z, z + n);
	m = unique(z, z + n) - z;
	for (int i = 0; i < n; i++)
		pt[i].z = lower_bound(z, z + m, pt[i].z) - z + 1;
	for (int i = 1; i <= m; i++)
		c[i] = 0;

	rec(0, n);

	int ans = 0;
	for (int i = 0; i < n; i++)
		ans = max(ans, pt[i].f);
	printf("%d\n", ans);
}

int main()
{
	while (~scanf("%d %d %d %d", &m, &n, &a, &b) && m + n + a + b)
	{
		init();
		solve();
	}
}