#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const double pi = 3.141592653589793;
template <class T>
using V = vector<T>;
using P = pair<long long, long long>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline void out(T a) {
  cout << a << '\n';
}
void YN(bool ok) {
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
const long long INF = 1e18;
const int mx = 200005;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  V<long long> cnt(m, 0);
  V<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i] % m]++;
  }
  priority_queue<long long, V<long long>, greater<long long>> que;
  V<V<int>> vec(m);
  long long ans = 0;
  for (int i = 0; i < 2 * m; i++) {
    if (cnt[i % m] < n / m) {
      while (que.size() && cnt[i % m] < n / m) {
        long long from = que.top();
        que.pop();
        long long cost = i % m - from;
        if (cost < 0) cost = cost + m;
        ans = ans + cost;
        vec[from].push_back(i % m);
        cnt[i % m]++;
      }
    } else if (cnt[i % m] > n / m) {
      while (cnt[i % m] > n / m) {
        que.push(i % m);
        cnt[i % m]--;
      }
    }
  }
  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    if (vec[a[i] % m].size()) {
      long long cost = vec[a[i] % m].back() - a[i] % m;
      if (cost < 0) cost += m;
      vec[a[i] % m].pop_back();
      a[i] += cost;
    }
    cout << a[i] << ' ';
  }
  cout << endl;
}
