#include <bits/stdc++.h>
using namespace std;
const int maxn = 100001;
const long long mod = 1000000007;
int main() {
  int n, m;
  cin >> n >> m;
  long long a[n];
  queue<long long> v[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v[a[i] % m].push(i);
  }
  long long moves = 0;
  queue<pair<long long, long long>> q;
  for (int i = 0; i < m; i++) {
    while (v[i].size() > n / m) {
      q.push(pair<int, int>(i, v[i].front()));
      v[i].pop();
    }
    while (v[i].size() != n / m && !q.empty()) {
      v[i].push(q.front().second);
      a[q.front().second] += (i - q.front().first + m) % m;
      moves += (i - q.front().first + m) % m;
      q.pop();
    }
  }
  for (int i = 0; i < m; i++) {
    while (v[i].size() != n / m && !q.empty()) {
      v[i].push(q.front().second);
      a[q.front().second] += (i - q.front().first + m) % m;
      moves += (i - q.front().first + m) % m;
      q.pop();
    }
  }
  cout << moves << endl;
  for (int i = 0; i < n; i++) cout << a[i] << " ";
}
