#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  vector<vector<int>> vec(k);
  long long mval = n / k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    vec[a[i] % k].push_back(i);
  }
  vector<pair<int, int>> free;
  long long ans = 0;
  long long t = 2;
  long long pk = 0;
  while (t--) {
    for (int i = 0; i < k; i++, pk++) {
      while (vec[i].size() > mval) {
        long long x = vec[i].back();
        vec[i].pop_back();
        free.push_back({x, i});
      }
      while (vec[i].size() < mval && !free.empty()) {
        long long pq = free.back().first;
        long long vj = free.back().second;
        free.pop_back();
        vec[i].push_back(pq);
        a[pq] += (pk - vj);
        ans += (pk - vj);
      }
    }
  }
  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
  int t = 1;
  while (t--) {
    solve();
  }
}
