#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k, a[200010];
  long long ans = 0;
  cin >> n >> m;
  k = n / m;
  vector<vector<pair<int, int>>> vec(m);
  vector<pair<int, int>> op;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    vec[x % m].push_back(make_pair(x, i));
  }
  for (int i = 0; i < 2 * m; i++) {
    int a = i % m;
    int b = (i + 1) % m;
    while (vec[a].size() > k) {
      op.push_back(make_pair(vec[a].rbegin()->first, vec[a].rbegin()->second));
      vec[a].pop_back();
    }
    while (vec[a].size() < k && !op.empty()) {
      int x = op.back().first;
      int y = op.back().second;
      int z = i - x % m;
      ans += z;
      vec[a].push_back(make_pair(x + z, y));
      op.pop_back();
    }
  }
  for (int i = 0; i < m; i++) {
    for (auto j : vec[i]) {
      a[j.second] = j.first;
    }
  }
  cout << ans << '\n';
  for (int i = 0; i < n; i++) {
    cout << a[i] << ' ';
  }
}
