#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  vector<vector<int>> remainder(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    remainder[a[i] % m].push_back(i);
  }
  vector<pair<int, int>> free;
  int max_num = n / m;
  long long int answer = 0;
  for (int i = 0; i < 2 * m; i++) {
    int cur_i = i % m;
    while (remainder[cur_i].size() > max_num) {
      int new_free = remainder[cur_i][remainder[cur_i].size() - 1];
      remainder[cur_i].pop_back();
      free.push_back({new_free, i});
    }
    while (remainder[cur_i].size() < max_num && free.size() > 0) {
      int new_id = free[free.size() - 1].first;
      int new_rem = free[free.size() - 1].second;
      free.pop_back();
      remainder[cur_i].push_back(new_id);
      answer += (m + cur_i - new_rem) % m;
      a[new_id] += (m + cur_i - new_rem) % m;
    }
  }
  cout << answer << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  return 0;
}
