1. Abridged Problem Statement
Given N friends and M pub bills S₁…S_M, at each pub they split the bill as equally as possible: each friend pays ⌊Sᵢ/N⌋ and exactly (Sᵢ mod N) friends pay one extra ruble. After all M pubs, friends' total payments differ by at most 1 ruble. Compute X, the number of friends who pay one ruble more than the others in total.

2. Detailed Editorial

Let N be the number of friends and S₁,…,S_M the bills.  At pub i:
- Everyone pays floorᵢ = ⌊Sᵢ/N⌋.
- rᵢ = Sᵢ mod N friends pay one additional ruble.

Over all pubs, each friend's total payment is
    Total_j = ∑₁≤i≤M floorᵢ  +  eⱼ,
where eⱼ is the number of times friend j was among those rᵢ extra-payers.

Define
    B = ∑₁≤i≤M floorᵢ,
    E = ∑₁≤i≤M rᵢ.

We need to assign these E extra-payers over N friends to minimize the maximum difference in Total_j.  Since B is common to everyone, we focus on distributing E extra rubles among N friends so that the difference between any two eⱼ is as small as possible.  By the "balancing lemma," the best you can do is give each friend either ⌊E/N⌋ or ⌊E/N⌋+1 extra rubles—this guarantees max(eⱼ) − min(eⱼ) ≤ 1, hence max(Total_j) − min(Total_j) ≤ 1.

Which friends get the "+1"?  Exactly R = E mod N of them.  Consequently, X = R.

But
    E = ∑ (Sᵢ mod N)
and
    ∑(Sᵢ mod N) ≡ ∑Sᵢ  (mod N).

Thus the answer is simply
    X = (∑₁≤i≤M Sᵢ) mod N.

Time Complexity: O(M).
Memory: O(1) extra.

3. C++ Solution

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int mod, n;
vector<int> a;

void read() {
    cin >> mod >> n;
    a.resize(n);
    cin >> a;
}

void solve() {
    // At each pub the remainder S_i mod N friends pay one extra ruble, and the
    // optimal global strategy lets these extra rubles cancel as a single running
    // count modulo N. So the number of friends who end up paying one ruble more
    // than the rest is the sum of all bills taken modulo N.

    int sum = 0;
    for(int i = 0; i < n; i++) {
        sum += a[i];
        sum %= mod;
    }

    cout << sum << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

4. Python Solution with Detailed Comments

```python
import sys

def main():
    data = sys.stdin.read().split()
    # First two numbers: N friends, M pubs
    N, M = map(int, data[:2])
    # Next M numbers: the bills S1..SM
    bills = map(int, data[2:])

    total_mod = 0
    # Sum each bill modulo N to avoid large sums
    for s in bills:
        total_mod = (total_mod + s) % N

    # The answer is (sum of all bills) mod N
    print(total_mod)

if __name__ == "__main__":
    main()
```

5. Compressed Editorial

Compute X = (∑ Sᵢ) mod N.  Rationale: at each pub i, you have rᵢ = Sᵢ mod N extra-payers.  Summing these gives E extra rubles; distributing E among N friends as evenly as possible yields exactly E mod N friends with one extra ruble overall.
