#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
const long long MOD = 1e9 + 7;
const long long N = 4e5 + 10;
const long long INF = 1e18 + 10;
long long n, m;
long long fix(long long x) { return (x % m + m) % m; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  long long need = n / m;
  vector<long long> v(n);
  vector<vector<long long> > a(m);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
    a[v[i] % m].push_back(i);
  }
  vector<long long> free;
  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    while ((long long)a[i].size() > need) {
      free.push_back(a[i].back());
      a[i].pop_back();
    }
    while ((long long)a[i].size() < need && not free.empty()) {
      long long idx = free.back();
      free.pop_back();
      a[i].push_back(idx);
      long long inc = fix(i - v[idx]);
      ans += inc;
      v[idx] += inc;
    }
  }
  for (long long i = 0; i < m; i++) {
    while ((long long)a[i].size() > need) {
      free.push_back(a[i].back());
      a[i].pop_back();
    }
    while ((long long)a[i].size() < need && not free.empty()) {
      long long idx = free.back();
      free.pop_back();
      a[i].push_back(idx);
      long long inc = fix(i - v[idx]);
      ans += inc;
      v[idx] += inc;
    }
  }
  for (int i = 0; i < m; i++) {
    assert((int)a[i].size() == need);
  }
  cout << ans << endl;
  for (long long i = 0; i < n; i++) {
    cout << v[i] << ' ';
  }
  cout << endl;
  return 0;
}
