#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll,ll> ii;
ll n,m,a[300000],b[300000],lua[300000],res,trace[300000];
queue<ll> A[300000];
vector<ll> kq;
ii D[300000];
int main()
{
cin >> n >> m;
for(int i = 1;i<=n;i++){
cin >> a[i];
b[i] = a[i] % m;
++lua[b[i]];
A[b[i]].push(a[i]);
D[i] = ii(a[i],i);
}
res = 0;
ll cur = n/m;
if(lua[m-1] > cur){
ll sl = lua[m-1] -cur;
lua[m-1] = cur;
trace[m-1] = sl;
lua[0]+=sl;
res+=sl;
}
for(int i = 0;i<m-1;i++){
if(lua[i] > cur && lua[i+1] <= cur){
res+= lua[i] - cur;
trace[i] = lua[i] - cur;
lua[i+1] += lua[i] - cur;
lua[i] = cur;
}
}
if(trace[m-1] != 0){
for(int i = 1;i<=trace[m-1];i++){
ll top = A[m-1].front();
A[m-1].pop();
A[0].push(top+1);
}
}
for(int i = 0;i<m-1;i++){
if(trace[i] != 0){
for(int j = 1;j<=trace[i];j++){
ll top = A[i].front();
A[i].pop();
A[i+1].push(top+1);
}
}
}
cout << res << "\n";
for(int i = 0;i<=m-1;i++){
while(A[i].size() > 0){
kq.push_back(A[i].front());
A[i].pop();
}
}
sort(kq.begin(),kq.end());
sort(D+1,D+n+1);
for(int i = 1;i<=n;i++)
trace[D[i].second] = kq[i-1];
for(int i = 1;i<=n;i++) cout << trace[i] << " ";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIGxsIGxvbmcgbG9uZwp0eXBlZGVmIHBhaXI8bGwsbGw+IGlpOwpsbCBuLG0sYVszMDAwMDBdLGJbMzAwMDAwXSxsdWFbMzAwMDAwXSxyZXMsdHJhY2VbMzAwMDAwXTsKcXVldWU8bGw+IEFbMzAwMDAwXTsKdmVjdG9yPGxsPiBrcTsKaWkgRFszMDAwMDBdOwppbnQgbWFpbigpCnsKICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IoaW50IGkgPSAxO2k8PW47aSsrKXsKICAgICAgICBjaW4gPj4gYVtpXTsKICAgICAgICBiW2ldID0gYVtpXSAlIG07CiAgICAgICAgKytsdWFbYltpXV07CiAgICAgICAgQVtiW2ldXS5wdXNoKGFbaV0pOwogICAgICAgIERbaV0gPSBpaShhW2ldLGkpOwogICAgfQogICAgcmVzID0gMDsKICAgIGxsIGN1ciA9IG4vbTsKICAgIGlmKGx1YVttLTFdID4gY3VyKXsKICAgICAgICBsbCBzbCA9IGx1YVttLTFdIC1jdXI7CiAgICAgICAgbHVhW20tMV0gPSBjdXI7CiAgICAgICAgdHJhY2VbbS0xXSA9IHNsOwogICAgICAgIGx1YVswXSs9c2w7CiAgICAgICAgcmVzKz1zbDsKICAgIH0KICAgIGZvcihpbnQgaSA9IDA7aTxtLTE7aSsrKXsKICAgICAgICBpZihsdWFbaV0gPiBjdXIgJiYgbHVhW2krMV0gPD0gY3VyKXsKICAgICAgICAgICAgcmVzKz0gbHVhW2ldIC0gY3VyOwogICAgICAgICAgICB0cmFjZVtpXSA9IGx1YVtpXSAtIGN1cjsKICAgICAgICAgICAgbHVhW2krMV0gKz0gbHVhW2ldIC0gY3VyOwogICAgICAgICAgICBsdWFbaV0gPSBjdXI7CiAgICAgICAgfQogICAgfQogICAgaWYodHJhY2VbbS0xXSAhPSAwKXsKICAgICAgICBmb3IoaW50IGkgPSAxO2k8PXRyYWNlW20tMV07aSsrKXsKICAgICAgICAgICAgbGwgdG9wID0gQVttLTFdLmZyb250KCk7CiAgICAgICAgICAgIEFbbS0xXS5wb3AoKTsKICAgICAgICAgICAgQVswXS5wdXNoKHRvcCsxKTsKICAgICAgICB9CiAgICB9CiAgICBmb3IoaW50IGkgPSAwO2k8bS0xO2krKyl7CiAgICAgICAgaWYodHJhY2VbaV0gIT0gMCl7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDE7ajw9dHJhY2VbaV07aisrKXsKICAgICAgICAgICAgICAgIGxsIHRvcCA9IEFbaV0uZnJvbnQoKTsKICAgICAgICAgICAgICAgIEFbaV0ucG9wKCk7CiAgICAgICAgICAgICAgICBBW2krMV0ucHVzaCh0b3ArMSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IHJlcyA8PCAiXG4iOwogICAgZm9yKGludCBpID0gMDtpPD1tLTE7aSsrKXsKICAgICAgICB3aGlsZShBW2ldLnNpemUoKSA+IDApewogICAgICAgICAgICBrcS5wdXNoX2JhY2soQVtpXS5mcm9udCgpKTsKICAgICAgICAgICAgQVtpXS5wb3AoKTsKICAgICAgICB9CiAgICB9CiAgICBzb3J0KGtxLmJlZ2luKCksa3EuZW5kKCkpOwogICAgc29ydChEKzEsRCtuKzEpOwogICAgZm9yKGludCBpID0gMTtpPD1uO2krKykKICAgICAgICB0cmFjZVtEW2ldLnNlY29uZF0gPSBrcVtpLTFdOwogICAgZm9yKGludCBpID0gMTtpPD1uO2krKykgY291dCA8PCB0cmFjZVtpXSA8PCAiICI7CiAgICByZXR1cm4gMDsKfQ==