#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1e5 + 5;
int x[maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int k = n / m;
  int i, j;
  vector<vector<int> > val(m);
  for (i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    val[x[i] % m].push_back(i);
  }
  vector<pair<int, int> > fre;
  long long ans = 0;
  for (i = 0; i < 2 * m; i++) {
    int cur = i % m;
    while (val[cur].size() > k) {
      int elm = val[cur].back();
      val[cur].pop_back();
      fre.push_back(make_pair(elm, i));
    }
    while (val[cur].size() < k && !fre.empty()) {
      int elm = fre.back().first;
      int mm = fre.back().second;
      fre.pop_back();
      val[cur].push_back(elm);
      ans += i - mm;
      x[elm] += i - mm;
    }
  }
  printf("%I64d\n", ans);
  for (i = 0; i < n; i++) printf("%d ", x[i]);
  printf("\n");
  return 0;
}
