#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int k = n / m;
  vector<int> a(n);
  vector<vector<int>> val(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    val[a[i] % m].push_back(i);
  }
  long long ans = 0;
  vector<pair<int, int>> fre;
  for (int i = 0; i < 2 * m; i++) {
    int cur = i % m;
    while (val[cur].size() > k) {
      int pos = val[cur].back();
      val[cur].pop_back();
      fre.push_back({pos, i});
    }
    while (val[cur].size() < k && !fre.empty()) {
      int pos = fre.back().first;
      int valu = fre.back().second;
      fre.pop_back();
      val[cur].push_back(pos);
      a[pos] += (i - valu);
      ans += (i - valu);
    }
  }
  cout << ans << '\n';
  for (int i = 0; i < n; i++) cout << a[i] << " ";
  return 0;
}
