#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > v;
long long int a[500005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t, n, m, q, i, j, k, x, y, z;
  cin >> n >> m;
  long long int d = (n / m);
  for (i = 0; i < n; i++) {
    cin >> x;
    a[i] = x;
    v.push_back(make_pair(x % m, i));
  }
  sort(v.begin(), v.end());
  long long int move = 0;
  i = 0;
  j = n - 1;
  x = 0;
  y = 0;
  while (y != m) {
    x++;
    long long int op1, op2;
    op1 = (y - v[i].first + m) % m;
    op2 = (y - v[j].first + m) % m;
    if (op1 < op2) {
      a[v[i].second] += op1;
      move += op1;
      i++;
    } else {
      a[v[j].second] += op2;
      move += op2;
      j--;
    }
    if (x == d) {
      x = 0;
      y++;
    }
  }
  cout << move << "\n";
  for (i = 0; i < n; i++) cout << a[i] << " ";
}
