#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int val = n / m;
  vector<vector<int>> v(m);
  for (int i = 0; i < n; i++) v[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 (int(v[cur].size()) > val) {
      int elem = v[cur].back();
      v[cur].pop_back();
      fre.push_back(make_pair(elem, i));
    }
    while (int(v[cur].size()) < val && !fre.empty()) {
      int elem = fre.back().first;
      int mmod = fre.back().second;
      fre.pop_back();
      v[cur].push_back(elem);
      a[elem] += i - mmod;
      ans += i - mmod;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < n; ++i) cout << a[i] << " ";
  cout << endl;
  return 0;
}
