#include <bits/stdc++.h>
using namespace std;
ostream cnull(NULL);
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cnull << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cnull << *sdbg++;
  cnull << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
const int MX = 2e5 + 7;
int a[MX], cnt[MX];
multiset<int> braki;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i) cin >> a[i], ++cnt[a[i] % m];
  int det = n / m;
  for (int i = (0); i <= ((int)(m)-1); ++i)
    for (int j = (0); j <= ((int)(det - cnt[i]) - 1); ++j) braki.insert(i);
  long long res = 0;
  for (int i = (1); i <= (n); ++i) {
    int rem = a[i] % m;
    if (cnt[rem] > det) {
      auto it = braki.lower_bound(rem);
      if (it == braki.end()) it = braki.begin();
      int dod = (m + *it - rem) % m;
      res += dod;
      --cnt[rem];
      a[i] += dod;
      ++cnt[*it];
      braki.erase(it);
    }
  }
  cout << res << '\n';
  for (int i = (1); i <= (n); ++i) cout << a[i] << ' ';
}
