#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433;
const long long M = 1e9;
map<long long, set<long long>> ms;
long long a[200100];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m, x, t = 0, s = 0, p, r;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    ms[a[i] % m].insert(i);
  }
  r = n / m;
  for (auto i : ms) {
    if (i.first == m - 1) break;
    if (i.second.size() > r) {
      p = i.second.size() - r;
      for (auto j : i.second) {
        p--;
        while ((ms[t].size() >= r || t <= i.first) && t < m - 1) t++;
        ms[t].insert(j);
        a[j] += (t - i.first);
        s += (t - i.first);
        ms[i.first].erase(j);
        if (p == 0) break;
      }
    }
  }
  t = 0;
  bool q = true;
  for (auto i : ms[m - 1]) {
    while (q && ms[t].size() >= r && t < m - 1) t++;
    if (t >= m - 1) break;
    if (q) {
      p = ms[t].size();
      q = false;
    }
    if (p < r) {
      a[i] += t + 1;
      s += t + 1;
      p++;
      if (p == r) {
        q = true;
        t++;
      }
    }
  }
  cout << s << endl;
  for (long long i = 0; i < n; i++) cout << a[i] << " ";
}
