#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
vector<int> mod[int(2e5) + 5];
int a[int(2e5) + 5];
int main() {
  int N, M;
  cin >> N >> M;
  int k = N / M;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    mod[a[i] % M].push_back(i);
  }
  vector<pair<int, int> > can;
  long long ans = 0;
  for (int i = 0; i < 2 * M; ++i) {
    int cur = i % M;
    while (mod[cur].size() > k) {
      int idx = mod[cur].back();
      mod[cur].pop_back();
      can.push_back(pair<int, int>(idx, i));
    }
    while (mod[cur].size() < k && !can.empty()) {
      int idx = can.back().first;
      int dmod = i - can.back().second;
      can.pop_back();
      mod[cur].push_back(idx);
      a[idx] += dmod;
      ans += dmod;
    }
  }
  cout << ans << '\n';
  for (int i = 0; i < N; ++i) cout << a[i] << ' ';
}
