#include <bits/stdc++.h>
long long int maxx(long long int a, long long int b) { return a > b ? a : b; }
long long int minn(long long int a, long long int b) { return a > b ? b : a; }
using namespace std;
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1, 0};
int dy[] = {-1, 0, 1, 1, -1, 0, 1, -1, 0};
void boostIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
long long int poww(long long int base, long long int exp) {
  base %= 1000000007;
  long long int result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % 1000000007;
    base = (base * base) % 1000000007;
    exp >>= 1;
  }
  return result;
}
long long int c[200200];
vector<int> a;
const int MX = 200200;
vector<pair<int, int>> numbers[MX];
vector<pair<int, int>> temp;
int nearst(int pos, int m, int rem) {
  if (pos > rem) {
    return pos - rem;
  } else {
    return pos + m - rem;
  }
}
int main() {
  boostIO();
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
    c[x % m]++;
    numbers[x % m].push_back(make_pair(x, i));
  }
  long long int ans = 0;
  long long int available = 0;
  int pos = -1;
  int req = n / m, num = 0;
  while (1) {
    pos = (pos + 1) % m;
    if (c[pos] > req) {
      available = available + (c[pos] - req);
      c[pos] = req;
      num = 0;
    } else if (c[pos] < req) {
      if (available >= (req - c[pos])) {
        available = available - (req - c[pos]);
        c[pos] = req;
      } else {
        c[pos] = c[pos] + available;
        available = 0;
      }
      num = 0;
    } else
      num++;
    ans = ans + available;
    if (num >= m) {
      break;
    }
  }
  cout << ans << endl;
  num = 0, pos = -1;
  while (1) {
    pos = (pos + 1) % m;
    if (numbers[pos].size() > req) {
      while (numbers[pos].size() > req) {
        temp.push_back(numbers[pos].back());
        numbers[pos].pop_back();
      }
      num = 0;
    } else if (numbers[pos].size() < req) {
      if (temp.size() >= (req - (int)numbers[pos].size())) {
        while (numbers[pos].size() < req) {
          int rem = temp.back().first % m;
          numbers[pos].push_back(make_pair(
              nearst(pos, m, rem) + temp.back().first, temp.back().second));
          temp.pop_back();
        }
      } else {
        while (!temp.empty()) {
          int rem = temp.back().first % m;
          numbers[pos].push_back(make_pair(
              nearst(pos, m, rem) + temp.back().first, temp.back().second));
          temp.pop_back();
        }
      }
      num = 0;
    } else
      num++;
    if (num >= m) {
      break;
    }
  }
  int res[MX];
  for (int i = 0; i < m; i++) {
    while (!numbers[i].empty()) {
      res[numbers[i].back().second] = numbers[i].back().first;
      numbers[i].pop_back();
    }
  }
  for (int i = 0; i < n; i++) cout << res[i] << " ";
}
