#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);++i)
typedef long long ll;
int a[2500000],b[2500000];
ll M,N,m0,md,n0,nd,ans=1e18,sa[2500001],sb[2500001];
ll val(ll i,ll j){
	return (M-i)*(N-j)+sa[i]+sb[j];
}
int main(){
	cin>>M>>N>>m0>>md>>n0>>nd;
	a[0]=m0;
	rep(i,M-1) a[i+1]=(a[i]*58+md)%(N+1);
	b[0]=n0;
	rep(i,N-1) b[i+1]=(b[i]*58+nd)%(M+1);
	sort(a,a+M);
	sort(b,b+N);
	rep(i,M) sa[i+1]=sa[i]+a[i];
	rep(i,N) sb[i+1]=sb[i]+b[i];
	ll j=N,now=sb[N];
	rep(i,M+1){
		while(j>0&&val(i,j)>val(i,j-1)){
			j--;
		}
		ans=min(ans,val(i,j));
	}
	cout<<ans<<endl;
}
### Prompt

Create a solution in Cpp for the following problem:

Example

Input

2 3 1 3 1 0


Output

2

### Response

```cpp
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);++i)
typedef long long ll;
int a[2500000],b[2500000];
ll M,N,m0,md,n0,nd,ans=1e18,sa[2500001],sb[2500001];
ll val(ll i,ll j){
	return (M-i)*(N-j)+sa[i]+sb[j];
}
int main(){
	cin>>M>>N>>m0>>md>>n0>>nd;
	a[0]=m0;
	rep(i,M-1) a[i+1]=(a[i]*58+md)%(N+1);
	b[0]=n0;
	rep(i,N-1) b[i+1]=(b[i]*58+nd)%(M+1);
	sort(a,a+M);
	sort(b,b+N);
	rep(i,M) sa[i+1]=sa[i]+a[i];
	rep(i,N) sb[i+1]=sb[i]+b[i];
	ll j=N,now=sb[N];
	rep(i,M+1){
		while(j>0&&val(i,j)>val(i,j-1)){
			j--;
		}
		ans=min(ans,val(i,j));
	}
	cout<<ans<<endl;
}
```