#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main() {
  int n, m;
  cin >> n >> m;
  int k = n / m;
  vector<int> a(n);
  vector<vector<int>> val(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    val[a[i] % m].push_back(i);
  }
  ll ans = 0;
  vector<pii> fre;
  for (int i = 0; i < 2 * m; ++i) {
    int cur = i % m;
    while (val[cur].size() > k) {
      fre.push_back(make_pair(val[cur].back(), i));
      val[cur].pop_back();
    }
    while (val[cur].size() < k && !fre.empty()) {
      int elem = fre.back().first;
      int mmod = fre.back().second;
      fre.pop_back();
      val[cur].push_back(elem);
      a[elem] += i - mmod;
      ans += i - mmod;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < n; ++i) cout << a[i] << ' ';
  return 0;
}
