#include <bits/stdc++.h>
using namespace std;
bool UDgreater(int a, int b) { return a > b; }
int mymin(int a, int b, int c, int d) { return min(a, min(b, min(c, d))); }
int main() {
  clock_t tStart = clock();
  long long n, m;
  cin >> n >> m;
  long long p = n / m;
  vector<long long> a(n);
  vector<vector<long long>> ostatki(m);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    ostatki[a[i] % m].push_back(i);
  }
  vector<vector<long long>> dif(n, vector<long long>(2));
  long long fIndex = 0;
  long long sIndex = 0;
  long long S = 0;
  for (long long t = 1; t <= 2; t++) {
    for (long long i = 0; i < m; i++) {
      while (((long long)ostatki[i].size()) - p > 0) {
        dif[fIndex][0] = ostatki[i].back();
        fIndex++;
        ostatki[i].pop_back();
      }
      while (p - ((long long)ostatki[i].size()) > 0 && sIndex < fIndex) {
        long long index = dif[sIndex][0];
        dif[sIndex][1] = i;
        if (i > (a[index] % m)) {
          S += (i - (a[index] % m));
          a[index] += (i - (a[index] % m));
        } else {
          S += (m + i - (a[index] % m));
          a[index] += (m + i - (a[index] % m));
        }
        sIndex++;
        ostatki[i].push_back(index);
      }
    }
  }
  cout << S << endl;
  for (long long i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  return 0;
}
