#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  long long a[200005];
  int r[200005];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    r[a[i] % m]++;
  }
  map<int, int> M;
  for (int i = 0; i < m; i++) {
    if (r[i] < n / m) {
      M[i] = n / m - r[i];
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (r[a[i] % m] > n / m) {
      auto lo = M.lower_bound(a[i] % m);
      if (lo == M.end()) lo = M.lower_bound(0);
      r[a[i] % m]--;
      r[lo->first]++;
      M[lo->first]--;
      ans += ((lo->first + n) - a[i] % m) % m;
      a[i] += ((lo->first + n) - a[i] % m) % m;
      if (M[lo->first] == 0) {
        M.erase(lo);
      }
    }
  }
  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    if (i) cout << " ";
    cout << a[i];
  }
  cout << endl;
  return 0;
}
