#include <bits/stdc++.h>
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
using namespace std;
class EllysRain
{
public:
std::vector<int> v;
int get_next(int P_prev, int M, int A, int L){
return (P_prev * M + A) % (L + 1);
}
bool check(int K, int D, int L){
std::vector<int> wow(v.begin(), v.begin()+K);
sort(all(wow));
if(wow.front() > D or wow.back() < L-D)
return 0;
for (int i = 0; i+1 < sz(wow); ++i)
{
if(wow[i+1]-wow[i] > D)
return 0;
}
return 1;
}
int getTime(int L, int D, int N, int P1, int M, int A){
for (int i = 1; i <= N; ++i)
{
v.pb(P1);
P1 = get_next(P1, M, A, L);
}
int low = 1, high = N, best = -1;
while(low <= high){
int mid = (low + high) >> 1;
if(check(mid, D, L)){
high = mid-1;
best = mid;
}
else{
low = mid + 1;
}
}
return best;
}
};
int main(){
int l, d, n, p1, m, a;
cin >> l >> d >> n >> p1 >> m >> a;
EllysRain x;
cout<< x.getTime(l, d, n, p1, m, a);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGFsbCh2KSB2LmJlZ2luKCksIHYuZW5kKCkKI2RlZmluZSBzeih2KSBpbnQodi5zaXplKCkpCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgRWxseXNSYWluCnsKcHVibGljOgoKCXN0ZDo6dmVjdG9yPGludD4gdjsKCglpbnQgZ2V0X25leHQoaW50IFBfcHJldiwgaW50IE0sIGludCBBLCBpbnQgTCl7CgkJcmV0dXJuIChQX3ByZXYgKiBNICsgQSkgJSAoTCArIDEpOwoJfQoKCWJvb2wgY2hlY2soaW50IEssIGludCBELCBpbnQgTCl7CgkJc3RkOjp2ZWN0b3I8aW50PiB3b3codi5iZWdpbigpLCB2LmJlZ2luKCkrSyk7CgkJc29ydChhbGwod293KSk7CgoJCWlmKHdvdy5mcm9udCgpID4gRCBvciB3b3cuYmFjaygpIDwgTC1EKQoJCQlyZXR1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