#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<long long> c(m, 0), a(n);
  set<long long> st;
  for (auto i = (0); i < (n); ++i) cin >> a[i];
  for (auto i = (0); i < (n); ++i) c[a[i] % m]++;
  for (auto i = (0); i < (m); ++i) {
    if (c[i] < n / m) st.insert(i);
  }
  long long ans = 0;
  for (auto i = (0); i < (n); ++i) {
    if (c[a[i] % m] <= n / m) continue;
    long long k = a[i] % m;
    auto x = st.upper_bound(k);
    c[k]--;
    if (x == st.end()) {
      x = st.begin();
      a[i] += m - k + *x;
      ans += m - k + *x;
    } else {
      a[i] += *x - k;
      ans += *x - k;
    }
    c[*x]++;
    if (c[*x] == n / m) st.erase(x);
  }
  cout << ans << "\n";
  for (auto i = (0); i < (n); ++i) cout << a[i] << ' ';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
