#include <bits/stdc++.h>
using namespace std;
int mod = 998244353;
const int valor = 5;
int oo = (1e9) + 7;
long long oo2 = 1ll * oo * oo;
int n, m;
int f(int pos1, int pos2) {
  return pos2 > pos1 ? pos2 - pos1 : m - (pos1 - pos2);
}
int main() {
  scanf("%d %d", &n, &m);
  vector<int> buc[3 * m + 1];
  vector<int> ext[3 * m + 1];
  vector<int> lk(3 * m + 1, 0);
  vector<long long> v;
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    v.push_back(x);
    int v = x % m;
    buc[v].push_back(i);
  }
  set<int> posicoes;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < ((int)buc[i].size()) - (n / m); ++j) {
      ext[i].push_back(buc[i][j]);
      ext[m + i].push_back(buc[i][j]);
    }
    lk[i] = n / m - buc[i].size();
    lk[m + i] = n / m - buc[i].size();
    if (lk[i] < 0) {
      lk[i] = 0;
      lk[m + i] = 0;
    }
    if (lk[i]) posicoes.insert(i);
    if (lk[i]) posicoes.insert(m + i);
  }
  int pos2 = m;
  int pos1 = 0;
  long long ans = 0;
  while (1) {
    while (pos1 < 2 * m and ext[pos1].size() == 0) pos1++;
    auto u = posicoes.lower_bound(pos1);
    if (u == posicoes.end()) break;
    pos2 = *u;
    if (pos2 >= 2 * m) break;
    if (pos1 >= 2 * m) break;
    while (ext[pos1].size() and lk[pos2]) {
      ans += f(pos1 % m, pos2 % m);
      v[ext[pos1].back()] += f(pos1 % m, pos2 % m);
      ext[pos1].pop_back();
      ext[(pos1 + m) % (2 * m)].pop_back();
      lk[pos2]--;
      lk[((pos2 + m) % (2 * m))]--;
    }
    if (lk[pos2] == 0) {
      posicoes.erase(pos2);
      posicoes.erase((pos2 + m) % (2 * m));
    }
  }
  printf("%lld\n", ans);
  for (int i = 0; i < n; ++i) {
    printf("%lld ", v[i]);
  }
  printf("\n");
}
