#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> arr(n);
  vector<long long> realArr(n);
  for (long long i = 0; i < n; i++) {
    cin >> realArr[i];
    arr[i].first = realArr[i] % m;
    arr[i].second = i;
  }
  sort(arr.begin(), arr.end());
  long long countStart = 0, countEnd = n - 1, num = 0, moves = 0;
  while (num < m) {
    long long same = n / m;
    while (same--) {
      if (arr[countStart].first > num) {
        realArr[arr[countEnd].second] += m - arr[countEnd].first + num;
        moves += m - arr[countEnd].first + num;
        countEnd--;
      } else {
        realArr[arr[countStart].second] += num - arr[countStart].first;
        moves += num - arr[countStart].first;
        countStart++;
      }
    }
    num++;
  }
  cout << moves << endl;
  if ((realArr).empty())
    cout << endl;
  else {
    cout << (realArr)[0];
    for (int out_i = 1; out_i < (realArr).size(); out_i++)
      cout << " " << (realArr)[out_i];
    cout << endl;
  }
  return 0;
}
