#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1123456;
const int MAXINT = 2147483098;
const long long MAXLL = 9223372036854775258LL;
vector<pair<long long, long long> > ababab;
vector<long long> v[MAXN];
long long a[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, m, k, ans = 0;
  cin >> n >> m;
  k = n / m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    v[a[i] % m].push_back(i);
  }
  for (int i = 0; i < 2 * m; ++i) {
    int now = i % m;
    while (v[now].size() > k) {
      ababab.push_back(make_pair(v[now].back(), i));
      v[now].pop_back();
    }
    while (v[now].size() < k && ababab.size() > 0) {
      int ind = ababab.back().first;
      int mod = ababab.back().second;
      ababab.pop_back();
      a[ind] += i - mod;
      ans += i - mod;
      v[now].push_back(ind);
    }
  }
  cout << ans << '\n';
  for (int i = 1; i <= n; ++i) cout << a[i] << " ";
  return 0;
}
