#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 10;
long long res = 0, n, a[maxN], need, m;
int dem[maxN];
struct IT {
  int n;
  vector<int> sum;
  IT(const int& _n) {
    n = _n;
    sum.assign(n + 1, 0);
    build();
  }
  void build() {
    for (int i = 1; i <= n; ++i) {
      int x = i;
      int q = x & (x - 1);
      sum[i] = (dem[i] < need);
      for (int ii = i - 1; ii > q; ii &= ii - 1) {
        sum[i] += sum[ii];
      }
    }
  }
  void update(int x) {
    for (; x <= n; x += (x & -x)) {
      --sum[x];
    }
  }
  int query(int x) {
    int res = 0;
    for (; x > 0; x &= (x - 1)) res += sum[x];
    return res;
  }
  long long BS(int c, int pos) {
    int l = 1;
    int h = n;
    while (l <= h) {
      int mid = (l + h) / 2;
      if (query(mid) < c)
        l = mid + 1;
      else
        h = mid - 1;
    }
    if (l > n || l <= 0) {
      return BS(1, pos);
    } else {
      dem[l]++;
      long long u = (l > pos ? l - pos : l - pos + m);
      res += u;
      if (dem[l] == need) update(l);
      return u;
    }
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  need = n / m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ++dem[a[i] % m + 1];
  }
  IT tree(m);
  for (int i = 1; i <= n; ++i) {
    if (dem[a[i] % m + 1] > need) {
      int k = a[i] % m + 1;
      dem[k]--;
      a[i] += tree.BS(tree.query(k) + 1, k);
    }
  }
  cout << res << "\n";
  for (int i = 1; i <= n; ++i) cout << a[i] << " ";
}
