#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  long long int a[200010];
  vector<int> r[200010];
  scanf(" %d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf(" %lld", &a[i]);
    r[a[i] % m].push_back(i);
  }
  long long int ans = 0;
  int goal = n / m, nearest = 0;
  for (int i = 0; i < m; i++) {
    while ((int)r[i].size() > goal) {
      nearest = max(nearest, i);
      while ((int)r[nearest % m].size() >= goal) nearest++;
      while (1) {
        if ((int)r[i].size() == goal || (int)r[nearest % m].size() == goal)
          break;
        r[nearest % m].push_back(r[i].back());
        if (i < nearest % m) {
          ans += nearest % m - i;
          a[r[i][(int)r[i].size() - 1]] += nearest % m - i;
        } else {
          ans += nearest % m - i + m;
          a[r[i][(int)r[i].size() - 1]] += nearest % m - i + m;
        }
        r[i].pop_back();
      }
    }
  }
  printf("%lld\n", ans);
  for (int i = 0; i < n; i++) {
    if (i == 0)
      printf("%lld", a[i]);
    else
      printf(" %lld", a[i]);
  }
  printf("\n");
  return 0;
}
