#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2>& p) {
  out << p.first << ' ' << p.second;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
  for (auto& x : v) in >> x;
  return in;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto x : v) out << x << ' ';
  return out;
}
long long n, m;
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
  return (a.first) % m < (b.first) % m;
}
int main() {
  iostream::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m;
  vector<pair<long long, long long> > a(n);
  for (long long i = (0); i < (n); ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort((a).begin(), (a).end(), cmp);
  long long req = n / m;
  long long curr = n - 1, cur1 = 0;
  vector<long long> cnt(m, 0);
  for (long long i = (0); i < (n); ++i) {
    cnt[(a[i].first) % m]++;
  }
  long long mv = 0;
  for (long long i = (0); i < (m); ++i) {
    while (cnt[i] < req) {
      while (cnt[(a[curr].first) % m] <= req) {
        curr--;
        if (curr == -1) curr = n - 1;
      }
      while (cnt[(a[cur1].first) % m] <= req) {
        cur1 = (cur1 + 1) % n;
      }
      long long c1, c2;
      if (i < (a[curr].first) % m) {
        c1 = m - ((a[curr].first) % m) + i;
      } else {
        c1 = i - ((a[curr].first) % m);
      }
      if (i < (a[cur1].first) % m) {
        c2 = m - ((a[cur1].first) % m) + i;
      } else {
        c2 = i - ((a[cur1].first) % m);
      }
      if (c1 < c2) {
        cnt[(a[curr].first) % m]--;
        mv += c1;
        a[curr].first += c1;
      } else {
        cnt[(a[cur1].first) % m]--;
        mv += c2;
        a[cur1].first += c2;
      }
      cnt[i]++;
    }
  }
  vector<long long> v(n);
  for (long long i = (0); i < (n); ++i) {
    v[a[i].second] = a[i].first;
  }
  cout << mv << endl;
  cout << v;
}
