#include <bits/stdc++.h>
using namespace std;
long long n, i = 0, m, lim, ans = 0, x, curr, a[200005];
queue<long long> q[200005];
vector<long long> v;
int main(void) {
  for (cin >> n >> m; i < n; i++) {
    cin >> a[i];
    q[a[i] % m].push(i);
  }
  lim = n / m;
  for (i = 0; i < 2 * m; i++) {
    curr = i % m;
    if (q[curr].size() > lim) {
      while (q[curr].size() > lim) {
        v.push_back(q[curr].front());
        q[curr].pop();
      }
    } else if (q[curr].size() < lim) {
      while (q[curr].size() < lim && v.size() > 0) {
        x = (curr - (a[v[v.size() - 1]] % m) + m) % m;
        ans += x;
        a[v[v.size() - 1]] += x;
        q[curr].push(a[v.size() - 1]);
        v.pop_back();
      }
    }
  }
  cout << ans << endl;
  for (i = 0; i < n; i++) cout << a[i] << " ";
  return 0;
}
