//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef long long LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF=100011122;
const double INFF=1e100;
const double eps=1e-8;
const LL mod=1000000007;
const int NN=300010;
const int MM=1000010;
/* ****************** */

const int limit=1000000;

int aa,bb;
int C = ~(1<<31);
int M = (1<<16)-1;
int rr() {
    aa = 36969 * (aa & M) + (aa >> 16);
    bb = 18000 * (bb & M) + (bb >> 16);
    return (C & ((aa << 16) + bb)) % 1000000;
}

struct node
{
    int x,y,z,id;
    bool operator<(const node &tt)const
    {
        return x<tt.x;
    }
}a[NN];
struct node1
{
    int x,y,z,id,yuan_id;
    bool operator<(const node1 &tt)const
    {
        if(y!=tt.y)return y<tt.y;
        return z>tt.z;
    }
}b[NN];
int dp[NN];
int c[2][limit+1];

int lowbit(int x)
{
    return x&(-x);
}
void update(int fg,int x,int val)
{
    for(;x<=limit;x+=lowbit(x))
        c[fg][x]=max(c[fg][x],val);
}
int get_max(int fg,int x)
{
    int ans=0;
    for(;x>0;x-=lowbit(x))
        ans=max(ans,c[fg][x]);
    return ans;
}
void clr_update(int fg,int x)
{
    for(;x<=limit;x+=lowbit(x))
        c[fg][x]=0;
}

void cdq(int l,int r)
{
    if(l==r)
    {
        dp[ a[l].id ]=max(dp[a[l].id],1);
        return;
    }
    int mid=(l+r)>>1;
    cdq(l,mid);
   // cdq(mid+1,r);

    int t,i,cnt=0,tx=a[mid+1].x;
    for(i=l;i<=r;i++)
    {
        b[cnt].x=a[i].x;
        b[cnt].y=a[i].y;
        b[cnt].z=a[i].z;
        b[cnt].id=i;
        b[cnt].yuan_id=a[i].id;
        cnt++;
    }

  //  printf("go to solve [%d,%d]\n",l,r);
   // memset(c,0,sizeof(c));

    sort(b,b+cnt);
    for(i=0;i<cnt;i++)
    {
       // printf("          yes? %d %d %d\n",b[i].x,b[i].y,b[i].z);

        if(b[i].id<=mid)
        {
           // printf("up [%d]->%d\n",b[i].z, dp[ b[i].yuan_id ]);

            update(1,b[i].z, dp[ b[i].yuan_id ] );
            if(b[i].x!=tx)
                update(0,b[i].z, dp[ b[i].yuan_id ] );
        }
        else
        {
            if(b[i].x==tx)
                t=get_max(0,b[i].z-1);
            else
                t=get_max(1,b[i].z-1);

           // printf("id==%d t==%d\n",b[i].yuan_id,t);

            dp[ b[i].yuan_id ] = max( dp[ b[i].yuan_id ] , t+1 );
        }
    }
    for(i=0;i<cnt;i++)
        if(b[i].id<=mid)
        {
            clr_update(1,b[i].z);
            if(b[i].x!=tx)
                clr_update(0,b[i].z);
        }

    cdq(mid+1,r);
}

void solve(int n)
{
    int i,ans=0;
    sort(a+1,a+1+n);
    for(i=1;i<=n;i++)
    {
        a[i].id=i;

      //  printf("-- %d %d %d\n",a[i].x,a[i].y,a[i].z);
    }

    memset(c,0,sizeof(c));
    memset(dp,0,sizeof(dp));

    cdq(1,n);

    for(i=1;i<=n;i++)
        ans=max(ans,dp[i]);

   // printf("ans==");
    printf("%d\n",ans);
}

int main()
{
    int m,n,i;
    while(scanf("%d%d%d%d",&m,&n,&aa,&bb)!=EOF)
    {
        if(m==0 && n==0)break;

        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
            a[i].x++;
            a[i].y++;
            a[i].z++;
        }
        for(i=1;i<=n;i++)
        {
            a[i+m].x=rr();
            a[i+m].y=rr();
            a[i+m].z=rr();

            a[i+m].x++;
            a[i+m].y++;
            a[i+m].z++;
        }

        solve(n+m);
    }
    return 0;
}

Measured memory_bytes: 20752