#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m;
vector<int> fre[N];
vector<pair<int, int> > tmp;
long long a[N];
void opf() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void inp() {
  cin >> n >> m;
  for (int(i) = (int)(1); (i) <= (int)(n); ++(i)) cin >> a[i];
}
void solve() {
  int k = n / m;
  for (int(i) = (int)(1); (i) <= (int)(n); ++(i)) fre[a[i] % m].push_back(i);
  long long res = 0;
  for (int(r) = (int)(0); (r) <= (int)(2 * m - 1); ++(r)) {
    int cur = r % m;
    while (fre[cur].size() > k) {
      int pos = fre[cur].back();
      fre[cur].pop_back();
      tmp.push_back(pair<int, int>(r, pos));
    }
    while (fre[cur].size() < k && tmp.size()) {
      int val = tmp.back().first;
      int pos = tmp.back().second;
      fre[cur].push_back(pos);
      tmp.pop_back();
      a[pos] += r - val;
      res += r - val;
    }
  }
  cout << res << '\n';
  for (int(i) = (int)(1); (i) <= (int)(n); ++(i)) cout << a[i] << ' ';
}
int main() {
  opf();
  inp();
  solve();
  return 0;
}
