#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#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,A,B;
        cin >> m >> n >> A >> B;
        a = A, b = 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){
            int xx = r();
            int yy = r();
            int zz = r();
            v[xx].pb(P(yy,zz));
        }
        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 = mx+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){
                            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;
}



Measured memory_bytes: 64880