#include <bits/stdc++.h>
using namespace std;
long long a[200005], cnt[200005];
vector<long long> v[200005];
int main() {
  long long n, m, mm, p, tot = 0, val;
  scanf("%lld", &n), scanf("%lld", &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    val = a[i] % m;
    v[val].push_back(i);
    cnt[val]++;
  }
  set<long long> st;
  p = n / m;
  for (long long i = 0; i < m; i++) {
    if (cnt[i] < p) {
      st.insert(i);
    }
  }
  for (long long i = 0; i < m; i++) {
    if (cnt[i] > p and st.size()) {
      long long dif = cnt[i] - p, k;
      cnt[i] = p;
      for (auto id : v[i]) {
        auto it = st.lower_bound((i + 1) % m);
        if (it == st.end()) it = st.begin();
        k = (*it - i + m) % m;
        a[id] += k;
        tot += k;
        dif--;
        cnt[*it]++;
        if (cnt[*it] == p) st.erase(it);
        if (dif == 0) break;
      }
    }
  }
  printf("%lld\n", tot);
  for (long long i = 1; i <= n; i++) {
    printf("%lld ", a[i]);
  }
  return 0;
}
