#include <bits/stdc++.h>
using namespace std;
long long arr[200005];
vector<vector<int> > rem(200001);
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
    rem[arr[i] % m].push_back(i);
  }
  long long ans = 0;
  int k = n / m;
  vector<pair<int, int> > free;
  for (int i = 0; i < 2 * m; i++) {
    int curr = i % m;
    while (rem[curr].size() > k) {
      free.push_back(make_pair(rem[curr].back(), i));
      rem[curr].pop_back();
    }
    while (rem[curr].size() < k && free.size() > 0) {
      int ele = free.back().first;
      int xnode = free.back().second;
      ans += i - xnode;
      arr[ele] += (i - xnode);
      rem[curr].push_back(ele);
      free.pop_back();
    }
  }
  cout << ans << endl;
  for (int i = 1; i <= n; i++) {
    printf("%lld ", arr[i]);
  }
}
