#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(n + 3);
  stack<int> indlist[m + 3];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    indlist[v[i] % m].push(i);
  }
  int k = n / m;
  stack<int> free;
  long long cnt = 0;
  for (int i = 0; i < 2 * m; i++) {
    int p = indlist[i % m].size();
    if (p > k) {
      while (p > k) {
        free.push(indlist[i % m].top());
        indlist[i % m].pop();
        p--;
      }
    } else {
      while (p < k) {
        if (free.empty()) {
          break;
        }
        int idx = free.top();
        cnt += 1LL * (i % m + m - (v[idx] % m)) % m;
        v[idx] += (i % m + m - (v[idx] % m)) % m;
        indlist[i % m].push(idx);
        free.pop();
        p++;
      }
    }
  }
  cout << cnt << '\n';
  for (int i = 0; i < n; i++) {
    cout << v[i] << ' ';
  }
  return 0;
}
