#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

int M,N,m0,n0,md,nd;

int main(){
  cin>>M>>N>>m0>>md>>n0>>nd;
  vector<ll> ms,summ(M+1,0);
  rep(i,M){
    ms.push_back(m0);
    m0=(m0*58+md)%(N+1);
  }
  sort(all(ms));
  rep(i,M)summ[i+1]=summ[i]+ms[i];

  vector<ll> ns,sumn(N+1,0);
  rep(i,N){
    ns.push_back(n0);
    n0=(n0*58+nd)%(M+1);
  }
  sort(all(ns));
  rep(i,N)sumn[i+1]=sumn[i]+ns[i];

  ll res=1e18;
  rep(i,M+1){
    ll j=lower_bound(all(ns),M-i)-ns.begin();
    minch(res,summ[i]+sumn[j]+(M-i)*(N-j));
  }
  cout<<res<<endl;

  return 0;
}


### Prompt

Develop a solution in cpp to the problem described below:

Example

Input

2 3 1 3 1 0


Output

2

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

int M,N,m0,n0,md,nd;

int main(){
  cin>>M>>N>>m0>>md>>n0>>nd;
  vector<ll> ms,summ(M+1,0);
  rep(i,M){
    ms.push_back(m0);
    m0=(m0*58+md)%(N+1);
  }
  sort(all(ms));
  rep(i,M)summ[i+1]=summ[i]+ms[i];

  vector<ll> ns,sumn(N+1,0);
  rep(i,N){
    ns.push_back(n0);
    n0=(n0*58+nd)%(M+1);
  }
  sort(all(ns));
  rep(i,N)sumn[i+1]=sumn[i]+ns[i];

  ll res=1e18;
  rep(i,M+1){
    ll j=lower_bound(all(ns),M-i)-ns.begin();
    minch(res,summ[i]+sumn[j]+(M-i)*(N-j));
  }
  cout<<res<<endl;

  return 0;
}


```