#include <bits/stdc++.h>
const int N = 1e6 + 5, MAX = 3e7, MOD = 998244353;
using namespace std;
int n, m;
long long a[N];
long long ans;
int occ[N];
std::vector<int> extra;
std::vector<int> pos[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]), occ[a[i] % m]++, pos[a[i] % m].push_back(i);
  int needed = n / m;
  for (int i = 0; i < 2 * m; i++) {
    int curr = i % m;
    while (occ[curr] > needed) {
      extra.push_back(curr);
      occ[curr]--;
    }
    while (occ[curr] < needed && ((int)(extra).size())) {
      int wa = extra.back();
      ans += i - wa;
      a[pos[wa].back()] += i - wa;
      pos[wa].pop_back();
      extra.pop_back();
      occ[curr]++;
    }
  }
  printf("%lld\n", ans);
  for (int i = 0; i < n; i++) printf("%lld ", a[i]);
  printf("\n");
}
