#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m;
vector<long long> a;
void solve() {
  long long cnt = 0;
  vector<vector<pair<int, int>>> ps;
  queue<pair<int, int>> buff;
  ps.resize(m);
  for (int i = 0; i < m; i++) ps[i].clear();
  for (int i = 0; i < n; i++) ps[a[i] % m].push_back({a[i], i});
  for (int i = 0; i < m; i++)
    if ((int)ps[i].size() > n / m) {
      for (int j = 0; j < m + m; j++) {
        int cur = (i + j) % m;
        while ((int)ps[cur].size() > n / m) {
          pair<int, int> v = ps[cur].back();
          ps[cur].pop_back();
          buff.push(v);
        }
        while ((int)ps[cur].size() < n / m && (int)buff.size() > 0) {
          pair<int, int> v = buff.front();
          buff.pop();
          int d = (cur + m - (v.first % m)) % m;
          v.first += (long long)d;
          ps[cur].push_back(v);
          cnt += (long long)d;
        }
      }
      break;
    }
  vector<long long> res;
  res.resize(n);
  for (int i = 0; i < m; i++) {
    assert((int)ps[i].size() == n / m);
    for (pair<int, int> j : ps[i]) {
      res[j.second] = j.first;
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    if (i < n - 1)
      cout << res[i] << " ";
    else
      cout << res[i] << endl;
  }
}
int main() {
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  solve();
  return 0;
}
