#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n = 0, m = 0, i = 0, j = 0, sum = 0;
  long long int k = 0, value = 0, valmod = 0, curmod = 0, pos = 0;
  cin >> n >> m;
  k = n / m;
  vector<long long int> vec(n);
  vector<vector<pair<long long int, long long int> > > mods(m);
  vector<pair<long long int, long long int> > temp;
  for (i = 0; i < n; i++) {
    cin >> vec[i];
    mods[vec[i] % m].push_back(pair<long long int, long long int>(vec[i], i));
  }
  for (i = 0; i < 2 * m; i++) {
    j = i % m;
    while (mods[j].size() > k) {
      temp.push_back(mods[j].back());
      mods[j].pop_back();
    }
    while (mods[j].size() < k && temp.size() != 0) {
      value = temp.back().first;
      valmod = value % m;
      pos = temp.back().second;
      if (j > valmod) {
        sum += j - valmod;
        vec[pos] += j - valmod;
        temp.pop_back();
      } else {
        sum += m - valmod + j;
        vec[pos] += m - valmod + j;
        temp.pop_back();
      }
      mods[j].push_back(pair<long long int, long long int>(vec[pos], pos));
    }
  }
  cout << sum << endl;
  for (i = 0; i < n; i++) cout << vec[i] << " ";
  return 0;
}
