#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k, l;
  cin >> n >> m;
  set<int> s;
  vector<int> a(n), c(m);
  int p = n / m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i] % m]++;
  }
  for (int i = 0; i < m; i++)
    if (c[i] < p) s.insert(i);
  long long ans = 0;
  for (int i = 0; i < n; i++)
    if (c[a[i] % m] > p) {
      long long cur = 0;
      auto po = s.lower_bound(a[i] % m);
      if (po == s.end()) {
        po = s.begin();
        cur = 1;
      }
      if (cur == 1) {
        cur = m - a[i] % m;
        cur += *po;
      }
      c[a[i] % m]--;
      c[*po]++;
      if (cur == 0) cur += abs(*po - a[i] % m);
      if (c[*po] == p) s.erase(*po);
      ans += cur;
      a[i] += cur;
    }
  cout << ans << endl;
  for (int i = 0; i < n; i++) cout << a[i] << " ";
}
