#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll m, n, m0, md, n0, nd;
vi sum_m, sum_n;

ll calc(int pm, int pn)
{
	return sum_m[pm] + sum_n[pn] + (m - pm)*(n - pn);
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	cin >> m >> n >> m0 >> md >> n0 >> nd;
	vector<int> v(m), w(n);
	v[0] = m0;
	REP(i, m - 1) v[i + 1] = (v[i] * 58 + md) % (n + 1);
	w[0] = n0;
	REP(i, n - 1) w[i + 1] = (w[i] * 58 + nd) % (m + 1);
	sort(ALL(v)); sort(ALL(w));
	sum_m.resize(m + 1); sum_n.resize(n + 1);
	REP(i, m) sum_m[i + 1] = sum_m[i] + v[i];
	REP(i, n) sum_n[i + 1] = sum_n[i] + w[i];
	ll ans = LLONG_MAX;
	int pm = 0, pn = n;
	while (1)
	{
		ll tmp = calc(pm, pn);
		chmin(ans, tmp);
		while (1)
		{
			if (pn == 0) break;
			ll nx = calc(pm, pn-1);
			if (tmp < nx) break;
			pn--;
			tmp = nx;
			chmin(ans, tmp);
		}
		pm++;
		if (pn < 0 || pm > m) break;
	}
	cout << ans << endl;
	return 0;
}
### Prompt

Construct a Cpp code solution to the problem outlined:

Example

Input

2 3 1 3 1 0


Output

2

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll m, n, m0, md, n0, nd;
vi sum_m, sum_n;

ll calc(int pm, int pn)
{
	return sum_m[pm] + sum_n[pn] + (m - pm)*(n - pn);
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	cin >> m >> n >> m0 >> md >> n0 >> nd;
	vector<int> v(m), w(n);
	v[0] = m0;
	REP(i, m - 1) v[i + 1] = (v[i] * 58 + md) % (n + 1);
	w[0] = n0;
	REP(i, n - 1) w[i + 1] = (w[i] * 58 + nd) % (m + 1);
	sort(ALL(v)); sort(ALL(w));
	sum_m.resize(m + 1); sum_n.resize(n + 1);
	REP(i, m) sum_m[i + 1] = sum_m[i] + v[i];
	REP(i, n) sum_n[i + 1] = sum_n[i] + w[i];
	ll ans = LLONG_MAX;
	int pm = 0, pn = n;
	while (1)
	{
		ll tmp = calc(pm, pn);
		chmin(ans, tmp);
		while (1)
		{
			if (pn == 0) break;
			ll nx = calc(pm, pn-1);
			if (tmp < nx) break;
			pn--;
			tmp = nx;
			chmin(ans, tmp);
		}
		pm++;
		if (pn < 0 || pm > m) break;
	}
	cout << ans << endl;
	return 0;
}
```