#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
typedef long long int ll;

const int limit=2500100;
ll M,N;
int m[limit],n[limit];
ll ms[limit],ns[limit];
ll md,nd;

int main(void){
	cin >> M >> N >> m[0] >> md >> n[0] >> nd;
	rep(i,M-1) m[i+1]=(m[i]*58+md)%(N+1);
	rep(i,N-1) n[i+1]=(n[i]*58+nd)%(M+1);
	sort(m,m+M+1);
	sort(n,n+N+1);
	rep(i,M) ms[i+1]=ms[i]+m[i+1];
	rep(i,N) ns[i+1]=ns[i]+n[i+1];

	ll ans=M*N;
	rep(i,M+1){
		ll cur=ms[i];
		int index=lower_bound(n,n+N+1,M-i)-n;
		if(index-1>=0) cur+=ns[index-1];
		cur+=(M-i)*(N-index+1);
		ans=min(ans,cur);
	}
	cout << ans << endl;
	return 0;
}
### Prompt

In Cpp, your task is to solve the following problem:

Example

Input

2 3 1 3 1 0


Output

2

### Response

```cpp
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
typedef long long int ll;

const int limit=2500100;
ll M,N;
int m[limit],n[limit];
ll ms[limit],ns[limit];
ll md,nd;

int main(void){
	cin >> M >> N >> m[0] >> md >> n[0] >> nd;
	rep(i,M-1) m[i+1]=(m[i]*58+md)%(N+1);
	rep(i,N-1) n[i+1]=(n[i]*58+nd)%(M+1);
	sort(m,m+M+1);
	sort(n,n+N+1);
	rep(i,M) ms[i+1]=ms[i]+m[i+1];
	rep(i,N) ns[i+1]=ns[i]+n[i+1];

	ll ans=M*N;
	rep(i,M+1){
		ll cur=ms[i];
		int index=lower_bound(n,n+N+1,M-i)-n;
		if(index-1>=0) cur+=ns[index-1];
		cur+=(M-i)*(N-index+1);
		ans=min(ans,cur);
	}
	cout << ans << endl;
	return 0;
}
```