#include <bits/stdc++.h>
using namespace std;
long long n, m, a[200005], ans;
vector<long long> f[200005], v;
void solve() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    f[a[i] % m].push_back(i);
  }
  long long k = n / m;
  for (long long i = 0; i < 2 * m + 1; i++) {
    long long cur = i % m;
    while ((long long)f[cur].size() > k) {
      v.push_back(f[cur].back());
      f[cur].pop_back();
    }
    while ((long long)f[cur].size() < k and (long long) v.size()) {
      long long d = v.back();
      v.pop_back();
      ans += (cur - (a[d] % m) + m) % m;
      a[d] += (cur - (a[d] % m) + m) % m;
      f[cur].push_back(d);
    }
  }
  cout << ans << '\n';
  for (long long i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
