#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename T>
istream& operator>>(istream& is, vector<T>& v)
{
    for (auto& e : v) {
        is >> e;
    }
    return is;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;

constexpr int MAX = 2500000;
int m[MAX + 1];
ll nsum[MAX + 1];
ll msum[MAX + 1];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll M, N, m0, md, n0, nd;
    cin >> M >> N >> m0 >> md >> n0 >> nd;
    // vector<int> m(M);
    // vector<ll> nsum(N);
    m[0] = m0;
    nsum[0] = n0;
    for (ll i = 1; i < M; i++) {
        m[i] = (m[i - 1] * 58 + md) % (N + 1);
    }
    for (ll i = 1; i < N; i++) {
        nsum[i] = (nsum[i - 1] * 58 + nd) % (M + 1);
    }
    // const ll SM = accumulate(m.begin(), m.end(), 0LL);
    const ll SM = accumulate(m, m + M, 0LL);
    sort(m, m + M, greater<ll>());
    sort(nsum, nsum + N);
    // sort(m.begin(), m.end(), greater<ll>());
    // sort(nsum.begin(), nsum.end());
    // show(m);
    // show(n);
    // vector<ll> msum(M + 1, 0LL);
    for (ll i = 1; i <= M; i++) {
        msum[i] = msum[i - 1] + m[i - 1];
    }
    for (ll i = 1; i < N; i++) {
        nsum[i] += nsum[i - 1];
    }
    nsum[N] = 0;
    // nsum.push_back(0LL);
    // sort(nsum.begin(), nsum.end());
    sort(nsum, nsum + N + 1);

    // show(msum);
    // show(nsum);
    ll mini = INF<ll>;
    for (ll i = 0; i <= N; i++) {
        ll sum = SM + nsum[i];
        mini = min(mini, sum + M * i);
        const ll value = N - i;
        const ll l = lower_bound(m, m + M, value, greater<int>{}) - m;
        // const ll l = lower_bound(m.begin(), m.end(), value, greater<int>{}) - m.begin();
        sum += -msum[l] + l * (N - i);
        mini = min(mini, sum);
    }
    cout << mini << 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>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename T>
istream& operator>>(istream& is, vector<T>& v)
{
    for (auto& e : v) {
        is >> e;
    }
    return is;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;

constexpr int MAX = 2500000;
int m[MAX + 1];
ll nsum[MAX + 1];
ll msum[MAX + 1];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll M, N, m0, md, n0, nd;
    cin >> M >> N >> m0 >> md >> n0 >> nd;
    // vector<int> m(M);
    // vector<ll> nsum(N);
    m[0] = m0;
    nsum[0] = n0;
    for (ll i = 1; i < M; i++) {
        m[i] = (m[i - 1] * 58 + md) % (N + 1);
    }
    for (ll i = 1; i < N; i++) {
        nsum[i] = (nsum[i - 1] * 58 + nd) % (M + 1);
    }
    // const ll SM = accumulate(m.begin(), m.end(), 0LL);
    const ll SM = accumulate(m, m + M, 0LL);
    sort(m, m + M, greater<ll>());
    sort(nsum, nsum + N);
    // sort(m.begin(), m.end(), greater<ll>());
    // sort(nsum.begin(), nsum.end());
    // show(m);
    // show(n);
    // vector<ll> msum(M + 1, 0LL);
    for (ll i = 1; i <= M; i++) {
        msum[i] = msum[i - 1] + m[i - 1];
    }
    for (ll i = 1; i < N; i++) {
        nsum[i] += nsum[i - 1];
    }
    nsum[N] = 0;
    // nsum.push_back(0LL);
    // sort(nsum.begin(), nsum.end());
    sort(nsum, nsum + N + 1);

    // show(msum);
    // show(nsum);
    ll mini = INF<ll>;
    for (ll i = 0; i <= N; i++) {
        ll sum = SM + nsum[i];
        mini = min(mini, sum + M * i);
        const ll value = N - i;
        const ll l = lower_bound(m, m + M, value, greater<int>{}) - m;
        // const ll l = lower_bound(m.begin(), m.end(), value, greater<int>{}) - m.begin();
        sum += -msum[l] + l * (N - i);
        mini = min(mini, sum);
    }
    cout << mini << endl;

    return 0;
}
```