#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int base = 19260817;
const int INF = 1e9;
const int maxn = 2e5 + 10;
const double eps = 1e-8;
int add(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}
int sub(int x, int y) {
  x -= y;
  return x < 0 ? x + mod : x;
}
int mul(int x, int y) { return 1LL * x * y % mod; }
int a[maxn], cnt[maxn];
vector<int> pos[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i] % m]++;
    pos[a[i] % m].push_back(i);
  }
  set<int> p, q;
  for (int i = 0; i < m; i++) {
    if (cnt[i] > n / m) p.insert(i);
    if (cnt[i] < n / m) q.insert(i);
  }
  long long ans = 0;
  while (!p.empty()) {
    int x = *p.begin(), y;
    auto it = q.upper_bound(x);
    if (it != q.end())
      y = *it;
    else
      y = *q.begin();
    int rx = cnt[x] - n / m, ry = n / m - cnt[y], num;
    if (rx <= ry) {
      ans += 1LL * (y - x + m) % m * rx;
      cnt[x] -= rx, cnt[y] += rx;
      p.erase(x);
      if (rx == ry) q.erase(y);
      num = rx;
    } else {
      ans += 1LL * (y - x + m) % m * ry;
      cnt[x] -= ry, cnt[y] += ry;
      q.erase(y);
      num = ry;
    }
    while (num) {
      a[pos[x].back()] += (y - x + m) % m;
      pos[x].pop_back();
      num--;
    }
  }
  cout << ans << endl;
  for (int i = 1; i <= n; i++) cout << a[i] << " ";
  return 0;
}
