#include <bits/stdc++.h>
using namespace std;
const long long int maxN = 201234;
vector<long long int> indexes[maxN];
vector<pair<long long int, long long int> > fre;
long long int vals[maxN];
long long int n, m, k;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  k = n / m;
  for (long long int i = 0; i < n; i++) {
    cin >> vals[i];
    indexes[vals[i] % m].push_back(i);
  }
  long long int final = 0;
  for (long long int i = 0; i < 2 * m; i++) {
    long long int cur = i % m;
    while (indexes[cur].size() > k) {
      long long int elem = indexes[cur].back();
      indexes[cur].pop_back();
      fre.push_back(make_pair(elem, i));
    }
    while (indexes[cur].size() < k && fre.size()) {
      long long int elem = fre.back().first;
      long long int mmod = fre.back().second;
      fre.pop_back();
      indexes[cur].push_back(elem);
      vals[elem] += i - mmod;
      final += i - mmod;
    }
  }
  cout << final << endl;
  for (long long int i = 0; i < n; i++) cout << vals[i] << " ";
  cout << endl;
  return 0;
}
