#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m;
long long a[N];
long long ans;
vector<long long> s[N];
set<long long> lot, nlt;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s[a[i] % m].push_back(i);
  }
  for (int i = 0; i < m; i++) {
    if (s[i].size() > n / m)
      lot.insert(i);
    else if (s[i].size() < n / m)
      nlt.insert(i);
  }
  while (!lot.empty()) {
    int pos = *lot.begin();
    while (!nlt.empty() && s[pos].size() > n / m) {
      set<long long>::iterator npi = nlt.upper_bound(pos);
      if (npi == nlt.end()) {
        npi = nlt.begin();
      }
      int np = *npi;
      while (s[pos].size() > n / m && s[np].size() < n / m) {
        s[np].push_back(s[pos].back());
        s[pos].pop_back();
        if (pos < np) {
          a[s[np].back()] += np - pos;
          ans += np - pos;
        } else {
          a[s[np].back()] += m - (pos - np);
          ans += m - (pos - np);
        }
      }
      if (s[np].size() == n / m) nlt.erase(npi);
    }
    lot.erase(lot.begin());
  }
  cout << ans << endl;
  for (int i = 0; i < n; i++) cout << a[i] << ' ';
  return 0;
}
