#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define MAXN 200000
struct node
{
    int num,pos;
}Ans[MAXN+5];
int m,n,cnt,A,ans,num;
vector<node> H[MAXN+5];
bool cmp(node s1,node s2){return s1.pos<s2.pos;}
int main()
{
    scanf("%d%d",&n,&m);
    cnt=n/m;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A);
        H[A%m].push_back((node){A,i});
    }
    for(int i=0;i<m*2;i++)
    {
        int Si=H[i%m].size();
        if(Si>cnt)
        for(int k=Si-1;k>=cnt;k--)
        {
            node tmp=H[i%m][k];
            tmp.num+=1;
            ans++;
            H[i%m].pop_back();
            H[(i+1)%m].push_back(tmp);
        }
    }
    for(int i=0;i<m;i++)
        for(int j=0;j<H[i].size();j++)
        Ans[++num]=H[i][j];
    sort(Ans+1,Ans+num+1,cmp);
    printf("%d\n",ans);
    for(int i=1;i<=n;i++)
        printf("%d ",Ans[i].num);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<iterator>
using namespace std;
#define MAXN 200000
struct node
{
    long long num;
    int pos;
}Ans[MAXN+5];
int m,n,cnt;
long long ans,A,num;
vector<node> H[MAXN+5];
bool cmp(node s1,node s2){return s1.pos<s2.pos;}
int main()
{
    scanf("%d%d",&n,&m);
    cnt=n/m;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&A);
        H[A%m].push_back((node){A,i});
    }
    for(int i=0;i<m*2;i++)
    {
        int Si=H[i%m].size();
        if(Si>cnt)
        for(int k=Si-1;k>=cnt;k--)
        {
            node tmp=H[i%m][k];
            tmp.num+=1;
            ans++;
            H[i%m].pop_back();
            vector<node>(H[i%m]).swap(H[i%m]);
            H[(i+1)%m].push_back(tmp);
        }
    }
    for(int i=0;i<m;i++)
        for(int j=0;j<H[i].size();j++)
        Ans[++num]=H[i][j];
    sort(Ans+1,Ans+num+1,cmp);
    printf("%lld\n",ans);
    for(int i=1;i<=n;i++)
        printf("%lld ",Ans[i].num);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<iterator>
using namespace std;
#define MAXN 200000
struct node
{
    long long num;
    int pos;
};
int m,n,cnt,pos;
long long ans,A,num;
vector<node> H[MAXN+5];
long long Ans[MAXN+5];
bool cmp(node s1,node s2){return s1.pos<s2.pos;}
int main()
{
    scanf("%d%d",&n,&m);
    cnt=n/m;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&Ans[i]);
        A=Ans[i];
        H[A%m].push_back((node){A,i});
    }
    for(int i=0;i<m;i++)
    {
        while(H[i%m].size()>cnt)
        {
            if(pos<i)pos=i;
            while(H[pos%m].size()>=cnt)pos++;
			int ice=min(H[i%m].size()-cnt,cnt-H[pos%m].size()),val=(pos%m-i+m)%m;
            while(ice--)
            {
                int mem=H[i%m].size();
                node tmp=H[i%m][mem-1];
                tmp.num+=val;
                Ans[tmp.pos]+=val;
                ans+=val;
                H[i%m].pop_back();
                vector<node>(H[i%m]).swap(H[i%m]);
                H[pos%m].push_back(tmp);
            }
        }
    }
    printf("%lld\n",ans);
    for(int i=1;i<=n;i++)
        printf("%lld ",Ans[i]);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<iterator>
using namespace std;
#define MAXN 200000
struct node
{
    int num,pos;
}Ans[MAXN+5];
int m,n,cnt,A,ans,num;
vector<node> H[MAXN+5];
bool cmp(node s1,node s2){return s1.pos<s2.pos;}
int main()
{
    scanf("%d%d",&n,&m);
    cnt=n/m;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&A);
        H[A%m].push_back((node){A,i});
    }
    for(int i=0;i<m*2;i++)
    {
        int Si=H[i%m].size();
        if(Si>cnt)
        for(int k=Si-1;k>=cnt;k--)
        {
            node tmp=H[i%m][k];
            tmp.num+=1;
            ans++;
            H[i%m].pop_back();
            vector<node>(H[i%m]).swap(H[i%m]);
            H[(i+1)%m].push_back(tmp);
        }
    }
    for(int i=0;i<m;i++)
        for(int j=0;j<H[i].size();j++)
        Ans[++num]=H[i][j];
    sort(Ans+1,Ans+num+1,cmp);
    for(int i=1;i<=n;i++)
        printf("%d ",Ans[i].num);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<iterator>
using namespace std;
#define MAXN 200000
struct node
{
    long long num;
    int pos;
};
int m,n,cnt,pos;
long long ans,A,num;
vector<node> H[MAXN+5];
long long Ans[MAXN+5];
bool cmp(node s1,node s2){return s1.pos<s2.pos;}
int main()
{
    scanf("%d%d",&n,&m);
    cnt=n/m;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&Ans[i]);
        A=Ans[i];
        H[A%m].push_back((node){A,i});
    }
    for(int i=0;i<m;i++)
    {
        while(H[i%m].size()>cnt)
        {
            if(pos<i)pos=i;
            while(H[pos%m].size()>=cnt)pos++;
			int ice=min(H[i%m].size()-cnt,cnt-H[pos%m].size()),val=(pos%m-i+m)%m;
            while(ice--)
            {
                int mem=H[i%m].size();
                node tmp=H[i%m][mem-1];
                tmp.num+=val;
                Ans[tmp.pos]+=val;
                ans+=val;
                H[i%m].pop_back();
                //vector<node>(H[i%m]).swap(H[i%m]);
                H[pos%m].push_back(tmp);
            }
        }
    }
    printf("%lld\n",ans);
    for(int i=1;i<=n;i++)
        printf("%lld ",Ans[i]);
}


#include <iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
using namespace std;

const int N = 200005;
set<int>w;
set<int>::iterator it;
int get(int k){
	it = w.lower_bound(k);
	if(it == w.end()){
		return *w.begin();
	}
	return *it;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m,t;
	cin>>n>>m;
	t = n/m;
	int a[N];
	int q[N];
	long long step = 0;
	memset(q,0,sizeof(q));
	for(int i = 0;i < n;i++){
		cin>>a[i];

		q[a[i] % m]++;
	}
	for(int i = 0;i < m;i++){
		if(q[i] < t){

			w.insert(i);
		}
	}
	for(int i = 0;i < n;i++){
		if(q[a[i] % m] > t){

			int k = get(a[i] % m);
			q[a[i] % m]--;
			q[k]++;

			if(q[k] == t){
				w.erase(k);
			}

			int s = (k+m-a[i]%m)%m;
			a[i] += s;
			step += s;
		}
	}
	cout<<step<<endl;
	for(int i = 0;i < n-1;i++){
		cout<<a[i]<<" ";
	}
	cout<<a[n-1]<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, m;
	cin >> n >> m;
	int k = n / m;
	vector<int> a(n);
	vector<vector<int> > val(m);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		val[a[i] % m].push_back(i);
	}

	long long ans = 0;
	vector<pair<int, int > > fre;
	for (int i = 0; i < 2 * m; ++i) {
		int cur = i % m;
		while (int(val[cur].size()) > k) {
			int elem = val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elem, i));
		}
		while (int(val[cur].size()) < k && !fre.empty()) {
			int elem = fre.back().first;
			int mmod = fre.back().second;
			fre.pop_back();
			val[cur].push_back(elem);
			a[elem] += i - mmod;
			ans += i - mmod;
		}
	}

	cout << ans << endl;
	for (int i = 0; i < n; ++i)
		cout << a[i] << " "<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int n, fa[maxn], k;
struct node {
    int x, y;
} qu[maxn];
struct p {
    int u, v, w;
    p() {}
    p(int u, int v, int w): u(u), v(v), w(w) {}
} cnt[maxn];
int cmp(p a, p b) {
    return a.w < b.w;
}
int Find(int x) {
    return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
int combine(int x, int y) {
    int nx = Find(x);
    int ny = Find(y);
    if(nx != ny) {
        fa[nx] = ny ;
        return 1;
    }
    return 0;
}
int kruskal() {
    int sum = 0, m = 0;
    for (int i = 0 ; i < n ; i++) fa[i] = i;
    for (int i = 0 ; i < k; i++) {
        if (combine(cnt[i].u, cnt[i].v)) {
            m++;
            sum += cnt[i].w;
            if (m == n - 1) break;
        }
    }
    return sum;
}
int main() {
    scanf("%d", &n);
    for (int i = 0 ; i < n ; i++ )
        scanf("%d%d", &qu[i].x, &qu[i].y);
    k = 0;
    for (int i = 0 ; i < n ; i++) {
        for (int j = i + 1 ; j < n ; j++) {
            cnt[k++] = p(i, j, abs(qu[i].x - qu[j].x) + abs(qu[i].y - qu[j].y));
        }
    }
    sort(cnt, cnt + k, cmp);
    printf("%d\n", kruskal());
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const int INF = 0x7fffffff;
LL n, m, num, ans, a[maxn], b[maxn];

int main() {
    scanf("%lld%lld", &n, &m);
    for (int i = 0 ; i < n ; i++) {
        scanf("%lld", &a[i]);
        b[a[i] % m]++;
    }
    set<LL>st;
    ans = 0, num = n / m;
    for (int i = 0 ; i < m ; i++)
        if (b[i] < num)   st.insert(i);
    for (int i = 0 ; i < n ; i++) {
        if (b[a[i] % m] <= num) continue;
        b[a[i] % m]--;
        set<LL>::iterator it;
        it = st.lower_bound(a[i] % m);
        if (it != st.end()) {
            ans += *it - a[i] % m;
            a[i] += *it - a[i] % m;
            b[*it]++;
            if (b[*it] == num) st.erase(it);
        } else {
            LL temp = m - a[i] % m;
            ans += *st.begin() + temp;
            a[i] += *st.begin() + temp;
            b[*st.begin()]++;
            if (b[*st.begin()] == num) st.erase(st.begin());
        }
    }
    printf("%lld\n", ans);
    for (int i = 0 ; i < n ; i++)
        printf("%lld ", a[i]);
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const int INF = 0x7fffffff;
LL n, m, a[maxn], cnt[maxn];

int main() {
    scanf("%lld%lld", &n, &m );
    for (int i = 0 ; i < n ; i++) {
        scanf("%lld", &a[i]);
        cnt[a[i] % m]++;
    }
    LL num = n / m, ans = 0;
    set<LL>st ;
    for (int i = 0 ; i < m ; i++)
        if (cnt[i] < num) st.insert(i);
    for (int i = 0 ; i < n ; i++) {
        if (cnt[a[i] % m] <= num) continue;
        set<LL>::iterator it;
        it = st.lower_bound(a[i] % m);
        cnt[a[i] % m]--;
        if (it == st.end()) {
            cnt[*st.begin()]++;
            LL temp = m - a[i] % m;
            ans += temp + (*st.begin());
            a[i] += temp + (*st.begin());
            if (cnt[*st.begin()] == num) st.erase(st.begin());
        } else {
            cnt[*it]++;
            ans += *it - (a[i] % m);
            a[i] += *it - (a[i] % m);
            if (cnt[*it] == num) st.erase(it);
        }
    }
    printf("%lld\n", ans);
    for (int i = 0 ; i < n ; i++)
        printf("%lld ", a[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <vector>
#define LL long long
#define F first
#define S second
#define VAL 200005

using namespace std;

int N, K, lim, i, j;
int v[VAL];
int nr[VAL], X, Y;
LL ANS, dif, M;
vector <int> G[VAL];
set <int> SUBLIM;
set <int> :: iterator it;

int main()
{
    cin >> N >> K;
    lim=N / K;
    for (i=1; i<=N; i++)
    {
        cin >> v[i];
        nr[v[i] % K]++;
        G[v[i] % K].push_back(i);
    }
    for (i=0; i<K; i++)
      if (nr[i]<lim)
        SUBLIM.insert(i);
    for (int cnt=1; cnt<=2; cnt++)
    {
        for (i=0; i<K; i++)
        {
            if (SUBLIM.empty()==true)
              break;
            while (nr[i]>lim)
            {
                it=SUBLIM.upper_bound(i);
                if (it==SUBLIM.end())
                  it=SUBLIM.begin();
                X=*it;
                M=lim-nr[X];
                if (nr[i]-lim>=M)
                  SUBLIM.erase(X);
                else
                  M=nr[i]-lim;
                dif=X-i;
                if (dif<0)
                  dif+=K;
                ANS+=dif*M;
                for (j=1; j<=M; j++)
                {
                    Y=G[i].back();
                    v[Y]+=dif;
                    G[i].pop_back();
                    G[X].push_back(Y);
                }
                nr[i]-=M;
                nr[X]+=M;
            }
        }
    }
    cout << ANS << '\n';
    for (i=1; i<=N; i++)
      cout << v[i] << " ";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <vector>
#define LL long long
#define F first
#define S second
#define VAL 200005

using namespace std;

int N, K, lim, i, j;
int v[VAL], X, M;
int nr[VAL], ANS;
vector <int> G[VAL];
vector <int> :: iterator it;

int main()
{
    cin >> N >> K;
    lim=N / K;
    for (i=1; i<=N; i++)
    {
        cin >> v[i];
        nr[v[i] % K]++;
        G[v[i] % K].push_back(i);
    }
    for (i=0; i<K; i++)
    {
        M=i+1;
        if (M==K)
          M=0;
        while (nr[i]>lim)
        {
            nr[i]--;
            ANS++;
            X=G[i].back();
            G[i].pop_back();
            v[X]++;
            nr[M]++;
            G[M].push_back(X);
        }
    }
    for (i=0; i<K; i++)
    {
        M=i+1;
        if (M==K)
          M=0;
        while (nr[i]>lim)
        {
            nr[i]--;
            ANS++;
            X=G[i].back();
            G[i].pop_back();
            v[X]++;
            nr[M]++;
            G[M].push_back(X);
        }
    }
    cout << ANS << '\n';
    for (i=1; i<=N; i++)
      cout << v[i] << " ";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <vector>
#define LL long long
#define F first
#define S second
#define VAL 200005

using namespace std;

LL N, K, lim, i, j;
LL v[VAL], X;
LL nr[VAL], ANS;
set <LL> G[VAL];
set <LL> :: iterator it;

int main()
{
    cin >> N >> K;
    lim=N / K;
    for (i=1; i<=N; i++)
    {
        cin >> v[i];
        nr[v[i] % K]++;
        G[v[i] % K].insert(i);
    }
    for (i=0; i<K; i++)
    {
        while (nr[i]>lim)
        {
            nr[i]--;
            ANS++;
            it=G[i].begin();
            X=*it;
            G[i].erase(it);
            v[X]++;
            nr[(i+1) % K]++;
            G[(i+1) % K].insert(X);
        }
    }
    for (i=0; i<K; i++)
    {
        while (nr[i]>lim)
        {
            nr[i]--;
            ANS++;
            it=G[i].begin();
            X=*it;
            G[i].erase(it);
            v[X]++;
            nr[(i+1) % K]++;
            G[(i+1) % K].insert(X);
        }
    }
    cout << ANS << '\n';
    for (i=1; i<=N; i++)
      cout << v[i] << " ";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n,m;
long long ans;
int main()
{
	cin>>n>>m;
	int k=n/m;
	vector<int> a(n);
	vector<vector<int> > rem(m);

	for(int i=0;i<n;i++){
		cin>>a[i];
		rem[a[i]%m].push_back(i);
	}

	vector<pair<int,int> > f;
	for(int i=0;i<2*m;i++){
		int cnt=i%m;
		while(int(rem[cnt].size())>k){
			int num=rem[cnt].back();
			rem[cnt].pop_back();
			f.push_back(make_pair(i,num));
		}
		while(int(rem[cnt].size())<k && !f.empty()){
			int mo=f.back().first;
			int num=f.back().second;
			rem[cnt].push_back(num);
			ans+=i-mo;
			a[num]+=i-mo;
			f.pop_back();
		}
	}

	cout<<ans<<endl;
	for(int i=0;i<n;i++) cout<<a[i]<<' ';
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
int ans;
int main()
{
	cin>>n>>m;
	int k=n/m;
	vector<int> a(n);
	vector<vector<int> > rem(m);

	for(int i=0;i<n;i++){
		cin>>a[i];
		rem[a[i]%m].push_back(i);
	}

	vector<pair<int,int> > f;
	for(int i=0;i<2*n;i++){
		int cnt=i%m;
		while(int(rem[cnt].size())>k){
			int num=rem[cnt].back();
			rem[cnt].pop_back();
			f.push_back(make_pair(i,num));
		}
		while(int(rem[cnt].size())<k){
			int mo=f.back().first;
			int num=f.back().second;
			rem[cnt].push_back(num);
			ans+=i-mo;
			a[num]+=i-mo;
			f.pop_back();
		}
	}

	cout<<ans<<endl;
	for(int i=0;i<n;i++) cout<<a[i]<<' ';
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,k;
	cin >> n >> k;
	string s;
	cin >> s;

	vector< pair <char,int> > c(n);
	for (int i=0;i<n;i++)
		c[i]=make_pair(s[i],i);
	sort(c.begin(),c.end());
	sort(c.begin()+k,c.end(),[&]( const pair<char,int> &a,const pair<char,int> &b){
		return a.second<b.second;
	});

	for (int i=k;i<n;i++)
		cout << c[i].first;
	cout << endl;
	return 0;
 }


#include <bits/stdc++.h>

using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	int k = n / m;
	vector<int> a(n);
	vector<vector<int> > val(m);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		val[a[i] % m].push_back(i);
	}
	long long ans = 0;
	vector<pair<int, int> > fre;
	for (int i = 0; i < 2 * m; ++i) {
		int cur = i % m;
		while (int(val[cur].size()) > k) {
			int elem = val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elem, i));
		}
		while (int(val[cur].size()) < k && !fre.empty()) {
			int elem = fre.back().first;
			int mmod = fre.back().second;
			fre.pop_back();
			val[cur].push_back(elem);
			a[elem] += i - mmod;
			ans += i - mmod;
		}
	}
	cout << ans << endl;
	for (int i = 0; i < n; ++i)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

vector <pair<ll,ll> > freed;
vector <ll> v(200005);
vector <ll> arr[200005];

int main()
{
    ll n,m;
    cin >> n >> m;

    ll k=n/m;
    for(ll i=0;i<n;i++)
    {
        ll a;
        cin >> a;
        v[i]=a;
        arr[a%m].push_back(i);
    }

    ll ans=0;
    for(ll i=0;i<2*m;i++)
    {
        ll cur=i%m;
        while(arr[cur].size()>k)
        {
            int elem=arr[cur].back();
            arr[cur].pop_back();
            freed.push_back(make_pair(elem,i));
        }
        while(arr[cur].size()<k&&!freed.empty())
        {
            int elem=freed.back().first;
            int ind=freed.back().second;
            freed.pop_back();
            arr[cur].push_back(elem);
            v[elem]+=i-ind;
            ans+=i-ind;
        }
    }
    cout << ans  << endl;
    for(ll i=0;i<n;i++)
    {
        cout << v[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

vector <pair<ll,ll> > freed(200005);
vector <ll> v(200005);
vector <ll> arr[200005];

int main()
{
    ll n,m;
    cin >> n >> m;

    ll k=n/m;
    for(ll i=0;i<n;i++)
    {
        ll a;
        cin >> a;
        v[i]=a;
        arr[a%m].push_back(i);
    }

    ll ans=0;
    for(ll i=0;i<2*m;i++)
    {
        ll cur=i%m;
        while(arr[cur].size()>k)
        {
            int elem=arr[cur].back();
            arr[cur].pop_back();
            freed.push_back(make_pair(elem,i));
        }
        while(arr[cur].size()<k)
        {
            int elem=freed.back().first;
            int ind=freed.back().second;
            freed.pop_back();
            arr[cur].push_back(elem);
            v[elem]+=i-ind;
            ans+=i-ind;
        }
    }
    cout << ans  << endl;
    for(ll i=0;i<n;i++)
    {
        cout << v[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,m;
    cin >> n >> m;
    vector <ll> v;
    vector <ll> arr[m];
    ll k=n/m;
    for(ll i=0;i<n;i++)
    {
        ll a;
        cin >> a;
        v.push_back(a);
        arr[a%m].push_back(i);
    }
    vector <pair<ll,ll> > free;
    ll ans=0;
    for(ll i=0;i<2*m;i++)
    {
        ll cur=i%m;
        while(arr[cur].size()>k)
        {
            int elem=arr[cur].back();
            arr[cur].pop_back();
            free.push_back(make_pair(elem,i));
        }
        while(arr[cur].size()<k)
        {
            int elem=free.back().first;
            int ind=free.back().second;
            free.pop_back();
            arr[cur].push_back(elem);
            v[elem]+=i-ind;
            ans+=i-ind;
        }
    }
    cout << ans  << endl;
    for(ll i=0;i<n;i++)
    {
        cout << v[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<pair<ll,ll> > vii;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pair<long long,long long> > vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<ll,pair<ll,ll> > iii;
#define PI 3.141592653589793
#define mod (ll)(1000*1000*1000+7)
//# define N 300005
template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, ll m){T ans=1; while(b>0){ if(b%2==1) ans=((ans%m)*(a%m))%m; b/=2; a=((a%m)*(a%m))%m; } return ans%m; }
# define INF (ll)1e18
# define LG 20
typedef pair<ll,pair<ll,ll> > pii;
# define INF1 (ll)1e9+2
# define maxn 200005
# define offset 1000005

vector<ll>g[maxn];
map<ll,ll>cnt1,cnt,need,cnt2;
ll a[maxn];

int main()
{
	IOS;
	ll n,m;
	cin >> n >> m;
	ll k=(n/m);
	set<ll>s;
	for(int i=0;i<m;i++)
		s.insert(i);

	for(int i=0;i<n;i++)
	{
		cin >> a[i];
		cnt1[a[i]%m]++;
		//trace2(a[i]%m,cnt1[a[i]%m]);
		if(cnt1[a[i]%m]==k && s.find(a[i]%m)!=s.end())
			s.erase(a[i]%m);
	}

   for(int i=0;i<m;i++)
   {
   	 need[i]=max(0LL,k-cnt1[i]);
   }

   ll ans=0;
   ll val1,val2;

	for(int i=0;i<n;i++)
	{
		cnt[a[i]%m]++;
		ll rem=(a[i]%m);
		//trace1(rem);
		if(cnt[a[i]%m]>k)
		{
           set<ll>::iterator it1=s.lower_bound(rem);
           if(it1==s.end())
           	val1=INF;
           else
           	val1=((*it1)-rem);
           set<ll>::iterator it2;
           if((ll)s.size()>0)
           {
	           	   it2=s.begin();
	           ll n2=(*it2);
	           if(n2<rem)
	           	 val2=(m-(rem-n2));
	           	else
	           		val2=(n2-rem);
           }
           else
           	val2=INF;
           //trace5(rem,(*it1),(*it2),val1,val2);
           	if(val1<=val2)
           	{
           		need[(*it1)]--;
           		if(need[(*it1)]==0)
           		  s.erase(it1);
           		ans+=val1;
           		g[rem].pb(val1);
           	}
           	else
           	{
           		need[(*it2)]--;
           		if(need[(*it2)]==0)
           		  s.erase(it2);
           		ans+=val2;
           		g[rem].pb(val2);
           	}
		}
	}

	cout << ans<<"\n";

	for(int i=0;i<n;i++)
	{
       ll rem=a[i]%m;
       cnt2[rem]++;
       if(cnt2[rem]>k)
       {
       	 ll sz=(ll)g[rem].size();
         a[i]+=g[rem][sz-1];
         g[rem].pop_back();
       }
	}

	for(int i=0;i<n;i++)
		cout << a[i]<<" ";
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<pair<ll,ll> > vii;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pair<long long,long long> > vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<ll,pair<ll,ll> > iii;
#define PI 3.141592653589793
#define mod (ll)(1000*1000*1000+7)
//# define N 300005
template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, ll m){T ans=1; while(b>0){ if(b%2==1) ans=((ans%m)*(a%m))%m; b/=2; a=((a%m)*(a%m))%m; } return ans%m; }
# define INF (ll)1e18
# define LG 20
typedef pair<ll,pair<ll,ll> > pii;
# define INF1 (ll)1e9+2
# define maxn 200005
# define offset 1000005

vector<int>g[26];
map<ll,ll>cnt1,cnt,need,cnt2;
ll a[maxn];

int main()
{
	IOS;
	ll n,m;
	cin >> n >> m;
	ll k=(n/m);
	set<ll>s;
	for(int i=0;i<m;i++)
		s.insert(i);

	for(int i=0;i<n;i++)
	{
		cin >> a[i];
		cnt1[a[i]%m]++;
		//trace2(a[i]%m,cnt1[a[i]%m]);
		if(cnt1[a[i]%m]==k)
			s.erase(a[i]%m);
	}

   for(int i=0;i<m;i++)
   {
   	 need[i]=max(0LL,k-cnt1[i]);
   }

   ll ans=0;
   ll val1,val2;

	for(int i=0;i<n;i++)
	{
		cnt[a[i]%m]++;
		ll rem=(a[i]%m);
		//trace1(rem);
		if(cnt[a[i]%m]>k)
		{
           set<ll>::iterator it1=s.lower_bound(rem);
           if(it1==s.end())
           	val1=INF;
           else
           	val1=((*it1)-rem);
           set<ll>::iterator it2;
           if((ll)s.size()>0)
           {
	           	   it2=s.begin();
	           ll n2=(*it2);
	           if(n2<rem)
	           	 val2=(m-(rem-n2));
	           	else
	           		val2=(n2-rem);
           }
           else
           	val2=INF;
           //trace5(rem,(*it1),(*it2),val1,val2);
           	if(val1<=val2)
           	{
           		need[(*it1)]--;
           		if(need[(*it1)]==0)
           		  s.erase(it1);
           		ans+=val1;
           		g[rem].pb(val1);
           	}
           	else
           	{
           		need[(*it2)]--;
           		if(need[(*it2)]==0)
           		  s.erase(it2);
           		ans+=val2;
           		g[rem].pb(val2);
           	}
		}
	}

	cout << ans<<"\n";

	for(int i=0;i<n;i++)
	{
       ll rem=a[i]%m;
       cnt2[rem]++;
       if(cnt2[rem]>k)
       {
       	 ll sz=(ll)g[rem].size();
         a[i]+=g[rem][sz-1];
         g[rem].pop_back();
       }
	}

	for(int i=0;i<n;i++)
		cout << a[i]<<" ";
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<pair<ll,ll> > vii;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pair<long long,long long> > vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<ll,pair<ll,ll> > iii;
#define PI 3.141592653589793
#define mod (ll)(1000*1000*1000+7)
//# define N 300005
template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, ll m){T ans=1; while(b>0){ if(b%2==1) ans=((ans%m)*(a%m))%m; b/=2; a=((a%m)*(a%m))%m; } return ans%m; }
# define INF (ll)1e18
# define LG 20
typedef pair<ll,pair<ll,ll> > pii;
# define INF1 (ll)1e9+2
# define maxn 200005
# define offset 1000005

vector<int>g[26];
map<ll,ll>cnt1,cnt,need,cnt2;
ll a[maxn];

int main()
{
	IOS;
	ll n,m;
	cin >> n >> m;
	ll k=(n/m);
	set<ll>s;
	for(int i=0;i<m;i++)
		s.insert(i);

	for(int i=0;i<n;i++)
	{
		cin >> a[i];
		cnt1[a[i]%m]++;
		//trace2(a[i]%m,cnt1[a[i]%m]);
		if(cnt1[a[i]%m]==k)
			s.erase(a[i]%m);
	}

   for(int i=0;i<m;i++)
   {
   	 need[i]=max(0LL,k-cnt1[i]);
   }

   ll ans=0;
   ll val1,val2;

	for(int i=0;i<n;i++)
	{
		cnt[a[i]%m]++;
		ll rem=(a[i]%m);
		//trace1(rem);
		if(cnt[a[i]%m]>k)
		{
           set<ll>::iterator it1=s.lower_bound(rem);
           if(it1==s.end())
           	val1=INF;
           else
           	val1=((*it1)-rem);
           set<ll>::iterator it2;
           if((ll)s.size()>0)
           {
	           	   it2=s.begin();
	           ll n2=(*it2);
	           if(n2<rem)
	           	 val2=(m-(rem-n2));
	           	else
	           		val2=(n2-rem);
           }
           else
           	val2=INF;
           //trace5(rem,(*it1),(*it2),val1,val2);
           	if(val1<=val2)
           	{
           		need[(*it1)]--;
           		if(need[(*it1)]==0)
           		  s.erase(it1);
           		ans+=val1;
           		g[rem].pb(val1);
           	}
           	else
           	{
           		need[(*it2)]--;
           		if(need[(*it2)]==0)
           		  s.erase(it2);
           		g[rem].pb(val2);
           	}
		}
	}

	cout << ans<<"\n";

	for(int i=0;i<n;i++)
	{
       ll rem=a[i]%m;
       cnt2[rem]++;
       if(cnt2[rem]>k)
       {
       	 ll sz=(ll)g[rem].size();
         a[i]+=g[rem][sz-1];
         g[rem].pop_back();
       }
	}

	for(int i=0;i<n;i++)
		cout << a[i]<<" ";
}


#include<iostream>
#include<vector>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	int n,m;
	cin>>n>> m;
	int k=n/m;
	vector<int>a(n);
	vector<vector<int> >val(m);
	for(int i=0;i<n;++i){
		cin>>a[i];
		val[a[i]%m].push_back(i);
	}
	long long ans=0;
	vector<pair<int,int> >fre;
	for(int i=0;i<m<<1;++i){
		int cur=i%m;
		while(int(val[cur].size())>k){
			int elem=val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elem,i));
		}
		while(int(val[cur].size())<k&&
		!fre.empty()){
			int elem=fre.back().first;
			int mmod=fre.back().second;
			fre.pop_back();
			val[cur].push_back(elem);
			a[elem]+=i-mmod;
			ans+=i-mmod;
		}
	}
	cout<<ans<<"\n";
	for(int i=0;i<n;++i)cout<<a[i]<<" ";
	cout<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cout<<n<<" "<<k<<endl;
	ll a[n];
	map<ll, vector<ll> > h;
	for(ll i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		h[a[i]%k].push_back(i);
		//cout<<a[i]%k<<" <- "<<i<<endl;
	}
	ll mx = INT_MIN;
	ll id = -1;
	//cout<<h[5].size()<<endl;
	for(ll i = 0; i < k; i++) {
		//cout<<h[i].size()<<endl;
		if(mx < (ll)h[i].size()) {
			mx = h[i].size();
			id = i;
			//cout<<mx<<" "<<id<<endl;
		}
	}
	//cout<<mx<<" "<<id<<endl;
	ll ans = 0;
	vector<pair<int, int> > fre;
	for (ll i = 0; i < 2 * k; ++i) {
		ll cur = i % k;
		while (int(h[cur].size()) > n/k) {
			int elem = h[cur].back();
			h[cur].pop_back();
			fre.push_back(make_pair(elem, i));
		}
		while (int(h[cur].size()) < n/k && !fre.empty()) {
			int elem = fre.back().first;
			int mmod = fre.back().second;
			fre.pop_back();
			h[cur].push_back(elem);
			a[elem] += i - mmod;
			ans += i - mmod;
		}
	}
	printf("%lld\n", ans);
	// ll res[n];
	// for(int i = 0; i < k; i++) {
	// 	for(int j = 0; j < h[i].size(); j++) {
	// 		if(a[h[i][j]]%k == i) {
	// 			res[h[i][j]] = a[h[i][j]];
	// 		} else {
	// 			ll vl = a[h[i][j]]%k - i;
	// 			res[h[i][j]] = a[h[i][j]] + vl;
	// 		}
	// 	}
	// }
	for(int i = 0; i < n; i++) {
		printf("%lld ", a[i]);
	}
	printf("\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cout<<n<<" "<<k<<endl;
	ll a[n];
	map<ll, vector<ll> > h;
	for(ll i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		h[a[i]%k].push_back(i);
		//cout<<a[i]%k<<" <- "<<i<<endl;
	}
	ll mx = INT_MIN;
	ll id = -1;
	//cout<<h[5].size()<<endl;
	for(ll i = 0; i < k; i++) {
		//cout<<h[i].size()<<endl;
		if(mx < (ll)h[i].size()) {
			mx = h[i].size();
			id = i;
			//cout<<mx<<" "<<id<<endl;
		}
	}
	//cout<<mx<<" "<<id<<endl;
	ll ans = 0;
	vector<pair<int, int>> fre;
	for (ll i = 0; i < 2 * k; ++i) {
		ll cur = i % m;
		while (int(h[cur].size()) > n/k) {
			int elem = h[cur].back();
			h[cur].pop_back();
			fre.push_back(make_pair(elem, i));
		}
		while (int(h[cur].size()) < n/k && !fre.empty()) {
			int elem = fre.back().first;
			int mmod = fre.back().second;
			fre.pop_back();
			h[cur].push_back(elem);
			a[elem] += i - mmod;
			ans += i - mmod;
		}
	}
	printf("%lld\n", ans);
	// ll res[n];
	// for(int i = 0; i < k; i++) {
	// 	for(int j = 0; j < h[i].size(); j++) {
	// 		if(a[h[i][j]]%k == i) {
	// 			res[h[i][j]] = a[h[i][j]];
	// 		} else {
	// 			ll vl = a[h[i][j]]%k - i;
	// 			res[h[i][j]] = a[h[i][j]] + vl;
	// 		}
	// 	}
	// }
	for(int i = 0; i < n; i++) {
		printf("%lld ", a[i]);
	}
	printf("\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cout<<n<<" "<<k<<endl;
	ll a[n];
	vector<vector<ll> > h(k);
	for(ll i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		h[a[i]%k].push_back(i);
		//cout<<a[i]%k<<" <- "<<i<<endl;
	}
	ll mx = INT_MIN;
	ll id = -1;
	//cout<<h[5].size()<<endl;
	for(ll i = 0; i < k; i++) {
		//cout<<h[i].size()<<endl;
		if(mx < (ll)h[i].size()) {
			mx = h[i].size();
			id = i;
			//cout<<mx<<" "<<id<<endl;
		}
	}
	//cout<<mx<<" "<<id<<endl;
	int i = id;
	ll c = 0;
	do {
		int j = (i+1)%k;
		while(h[i].size() > n/k) {
			h[j].push_back(h[i].back());
			a[h[i].back()]++;
			h[i].pop_back();
			c++;
		}
		i = j;
	} while(i != id);
	printf("%lld\n", c);
	// ll res[n];
	// for(int i = 0; i < k; i++) {
	// 	for(int j = 0; j < h[i].size(); j++) {
	// 		if(a[h[i][j]]%k == i) {
	// 			res[h[i][j]] = a[h[i][j]];
	// 		} else {
	// 			ll vl = a[h[i][j]]%k - i;
	// 			res[h[i][j]] = a[h[i][j]] + vl;
	// 		}
	// 	}
	// }
	for(int i = 0; i < n; i++) {
		printf("%lld ", a[i]);
	}
	printf("\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cout<<n<<" "<<k<<endl;
	ll a[n];
	map<ll, vector<ll> > h;
	for(ll i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		h[a[i]%k].push_back(i);
		//cout<<a[i]%k<<" <- "<<i<<endl;
	}
	ll mx = INT_MIN;
	ll id = -1;
	//cout<<h[5].size()<<endl;
	for(ll i = 0; i < k; i++) {
		//cout<<h[i].size()<<endl;
		if(mx < (ll)h[i].size()) {
			mx = h[i].size();
			id = i;
			//cout<<mx<<" "<<id<<endl;
		}
	}
	//cout<<mx<<" "<<id<<endl;
	int i = id;
	ll c = 0;
	do {
		int j = (i+1)%k;
		while(h[i].size() > n/k) {
			h[j].push_back(h[i].back());
			a[h[i].back()]++;
			h[i].pop_back();
			c++;
		}
		i = j;
	} while(i != id);
	printf("%lld\n", c);
	// ll res[n];
	// for(int i = 0; i < k; i++) {
	// 	for(int j = 0; j < h[i].size(); j++) {
	// 		if(a[h[i][j]]%k == i) {
	// 			res[h[i][j]] = a[h[i][j]];
	// 		} else {
	// 			ll vl = a[h[i][j]]%k - i;
	// 			res[h[i][j]] = a[h[i][j]] + vl;
	// 		}
	// 	}
	// }
	for(int i = 0; i < n; i++) {
		printf("%lld ", a[i]);
	}
	printf("\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cout<<n<<" "<<k<<endl;
	ll a[n];
	map<ll, vector<ll> > h;
	for(ll i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		h[a[i]%k].push_back(i);
		//cout<<a[i]%k<<" <- "<<i<<endl;
	}
	ll mx = INT_MIN;
	ll id = -1;
	//cout<<h[5].size()<<endl;
	for(ll i = 0; i < k; i++) {
		//cout<<h[i].size()<<endl;
		if(mx < (ll)h[i].size()) {
			mx = h[i].size();
			id = i;
			//cout<<mx<<" "<<id<<endl;
		}
	}
	//cout<<mx<<" "<<id<<endl;
	int i = id;
	ll c = 0;
	do {
		int j = (i+1)%k;
		//cout<<i<<"->"<<j<<" ";
		while(h[i].size() > n/k) {
			if(h[j].size() < n/k) {
				h[j].push_back(h[i].back());
				ll vl = ((j - i)%k + k) % k;
				a[h[i].back()] += vl;
				h[i].pop_back();
				c += vl;
			} else {
				j = (j+1)%k;
				//cout<<j<<" ";
				if(j == id) {
					break;
				}
			}
			//c++;
		}
		//cout<<endl;
		i = j;
	} while(i != id);
	printf("%lld\n", c);
	// ll res[n];
	// for(int i = 0; i < k; i++) {
	// 	for(int j = 0; j < h[i].size(); j++) {
	// 		if(a[h[i][j]]%k == i) {
	// 			res[h[i][j]] = a[h[i][j]];
	// 		} else {
	// 			ll vl = a[h[i][j]]%k - i;
	// 			res[h[i][j]] = a[h[i][j]] + vl;
	// 		}
	// 	}
	// }
	for(int i = 0; i < n; i++) {
		printf("%lld ", a[i]);
	}
	printf("\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cout<<n<<" "<<k<<endl;
	ll a[n];
	map<ll, vector<ll> > h;
	for(ll i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		h[a[i]%k].push_back(i);
		//cout<<a[i]%k<<" <- "<<i<<endl;
	}
	ll mx = INT_MIN;
	ll id = -1;
	//cout<<h[5].size()<<endl;
	for(ll i = 0; i < k; i++) {
		//cout<<h[i].size()<<endl;
		if(mx < (ll)h[i].size()) {
			mx = h[i].size();
			id = i;
			//cout<<mx<<" "<<id<<endl;
		}
	}
	//cout<<mx<<" "<<id<<endl;
	int i = id;
	ll c = 0;
	do {
		int j = (i+1)%n;
		while(h[i].size() > n/k) {
			h[j].push_back(h[i].back());
			a[h[i].back()]++;
			h[i].pop_back();
			c++;
		}
		i = j;
	} while(i != id);
	printf("%lld\n", c);
	// ll res[n];
	// for(int i = 0; i < k; i++) {
	// 	for(int j = 0; j < h[i].size(); j++) {
	// 		if(a[h[i][j]]%k == i) {
	// 			res[h[i][j]] = a[h[i][j]];
	// 		} else {
	// 			ll vl = a[h[i][j]]%k - i;
	// 			res[h[i][j]] = a[h[i][j]] + vl;
	// 		}
	// 	}
	// }
	for(int i = 0; i < n; i++) {
		printf("%lld ", a[i]);
	}
	printf("\n");
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=2e5+6;

set<ll>st;

ll n,m,cnt[N],arr[N],res,val,ans;

int main()
{
    cin>>n>>m;
    for(int i=0;i<m;cnt[i++]=n/m)st.insert((ll)i);

    for(int i=0;i<n;i++)
	{
        cin>>arr[i];
        res=arr[i]%m;

        if(res>*st.rbegin())	val=*st.begin();
        else val= *st.lower_bound(res);

        ans=ans+(val-res+m)%m;

        if(!--cnt[val])	st.erase(val);

        arr[i]=arr[i]+(val-res+m)%m;
    }
    cout<<ans<<endl;

    for(int i=0;i<n;i++)cout<<arr[i]<<" ";

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<bitset>
using namespace std;
typedef long long ll;

const ll maxn = 5e5+10;
const ll mo = 1e9+7;

ll b[500000];
ll a[500000];
queue<ll> d[500000];
int  main(){
    ll n,m,r,j,k,i,len,pd = 0,t = 0,q  = 0,w = 1,e;
    scanf("%lld %lld",&n,&m);
    for(i = 0;i < n;i++)
    {
        scanf("%lld",&b[i]);
        a[b[i]%m]++;
    }
    r = n / m;
    for(i = 0;i < m;i++)
    {
        if(a[i] > r)
        {
            q = i;
            if(w > 1)
                w--;
            for(j = w;j < m;j++)
            {
                ll t1 = r - a[(q+j) % m],t2 = a[i] - r;
                if(a[(q+j) % m] < r)
                {
                    printf("%lld %lld %lld\n",t1,t2,j);
                    if(t1 > t2)
                    {
                        t += t2 * j;
                        for(k = 0;k < t2;k++)
                            d[i].push(j);
                        a[(q+j) % m] += t2;
                        a[i] = r;
                    }
                    else
                    {
                        w++;
                        t += t1 * j;
                        for(k = 0;k < t1;k++)
                            d[i].push(j);
                        a[i] -= t1;
                        a[(q+j) % m] = r;
                    }
                    if(a[i] == r)
                        break;
                }
                else
                    w++;
            }
        }
    }
    for(i = 0;i < n;i++)
    {
        if(!d[b[i] % m].empty())
        {
            ll tp = d[b[i] % m].front();
            d[b[i] % m].pop();
            b[i]+=tp;
        }
         //   printf("%lld %lld\n",i,b[i]);
    }
    printf("%lld\n",t);
    printf("%lld",b[0]);
    for(i = 1 ;i < n;i++)
        printf(" %lld",b[i]);
    printf("\n");
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<bitset>
using namespace std;
typedef long long ll;

const ll maxn = 5e5+10;
const ll mo = 1e9+7;

ll b[200010];
ll a[200010];
queue<ll> d[200010];
int  main(){
    ll n,m,r,j,k,i,len,pd = 0,t = 0,q  = 0,w = 1,e;
    scanf("%lld %lld",&n,&m);
    for(i = 0;i < n;i++)
    {
        scanf("%lld",&b[i]);
        a[b[i]%m]++;
    }
    r = n / m;
    for(i = 0;i < m;i++)
    {
        if(a[i] > r)
        {
            q = i;
            if(w > 1)
                w--;
            for(j = w;j < m;j++)
            {
                ll t1 = r - a[(q+j) % m],t2 = a[i] - r;
                if(a[(q+j) % m] < r)
                {
                    printf("%lld %lld %lld\n",t1,t2,j);
                    if(t1 > t2)
                    {
                        t += t2 * j;
                        for(k = 0;k < t2;k++)
                            d[i].push(j);
                        a[(q+j) % m] += t2;
                        a[i] = r;
                    }
                    else
                    {
                        w++;
                        t += t1 * j;
                        for(k = 0;k < t1;k++)
                            d[i].push(j);
                        a[i] -= t1;
                        a[(q+j) % m] = r;
                    }
                    if(a[i] == r)
                        break;
                }
                else
                    w++;
            }
        }
    }
    for(i = 0;i < n;i++)
    {
        if(!d[b[i] % m].empty())
        {
            ll tp = d[b[i] % m].front();
            d[b[i] % m].pop();
            b[i]+=tp;
        }
         //   printf("%lld %lld\n",i,b[i]);
    }
    printf("%lld\n",t);
    printf("%lld",b[0]);
    for(i = 1 ;i < n;i++)
        printf(" %lld",b[i]);
    printf("\n");
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<bitset>
using namespace std;
typedef long long ll;

const ll maxn = 5e5+10;
const ll mo = 1e9+7;

ll b[200010];
ll a[200010];
queue<ll> d[200010];
int  main(){
    ll n,m,r,j,k,i,len,pd = 0,t = 0,q  = 0,w = 1,e;
    scanf("%lld %lld",&n,&m);
    for(i = 0;i < n;i++)
    {
        scanf("%lld",&b[i]);
        a[b[i]%m]++;
    }
    r = n / m;
    for(i = 0;i < m;i++)
    {
            if(w > 1)
                w--;
        if(a[i] > r)
        {
            q = i;
            for(j = w;j < m;j++)
            {
                ll t1 = r - a[(q+j) % m],t2 = a[i] - r;
                if(a[(q+j) % m] < r)
                {
                  //  printf("%lld %lld %lld\n",t1,t2,j);
                    if(t1 > t2)
                    {
                        t += t2 * j;
                        for(k = 0;k < t2;k++)
                            d[i].push(j);
                        a[(q+j) % m] += t2;
                        a[i] = r;
                    }
                    else
                    {
                        w++;
                        t += t1 * j;
                        for(k = 0;k < t1;k++)
                            d[i].push(j);
                        a[i] -= t1;
                        a[(q+j) % m] = r;
                    }
                    if(a[i] == r)
                        break;
                }
                else
                    w++;
            }
        }
    }
    for(i = 0;i < n;i++)
    {
        if(!d[b[i] % m].empty())
        {
            ll tp = d[b[i] % m].front();
            d[b[i] % m].pop();
            b[i]+=tp;
        }
         //   printf("%lld %lld\n",i,b[i]);
    }
    printf("%lld\n",t);
    printf("%lld",b[0]);
    for(i = 1 ;i < n;i++)
        printf(" %lld",b[i]);
    printf("\n");
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<stack>
#include<math.h>
#include<bitset>
using namespace std;
typedef long long ll;

const ll maxn = 5e5+10;
const ll mo = 1e9+7;
ll pm(ll x, ll n, ll m)
{
    ll res = 1;
    while (n > 0){
        if  (n & 1)
            res = (res * x) % m;
        x = (x * x) % m;
        n >>= 1;
    }
    return res;
}
ll a[500000];
ll b[500000];
ll c[500000];
int  main(){
    ll n,m,r,j,k,i,len,pd = 0,t = 0;
    scanf("%lld %lld",&n,&m);
    for(i = 0;i < n;i++)
    {
        scanf("%lld",&b[i]);
        a[b[i]%m]++;
    }
    r = n / m;
    for(pd = 0;pd <2;pd++)
    for(i = 0;i < m;i++)
    {
        if(a[i] > r)
        {
            c[i] += a[i] - r;
            t += a[i] - r;
            a[(i+1) % m] += a[i] - r;
            a[i] = r;
        }
    }
    for(pd = 0;pd < 2;pd++)
    {
        for(i = 0;i < n;i++)
        {
            if(c[b[i] % m])
            {
                c[b[i] % m]--;
                b[i]++;
            }
        }
    }
    printf("%lld\n",t);
    printf("%lld",b[0]);
    for(i = 1 ;i < n;i++)
        printf(" %lld",b[i]);
    printf("\n");
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<stack>
#include<math.h>
#include<bitset>
using namespace std;
typedef long long ll;

const ll maxn = 5e5+10;
const ll mo = 1e9+7;

ll a[500000];
ll b[500000];
ll c[500000];
int  main(){
    ll n,m,r,j,k,i,len,pd = 0,t = 0;
    scanf("%lld %lld",&n,&m);
    for(i = 0;i < n;i++)
    {
        scanf("%lld",&b[i]);
        a[b[i]%m]++;
    }
    r = n / m;
    for(pd = 0;pd <2;pd++)
    for(i = 0;i < m;i++)
    {
        if(a[i] > r)
        {
            c[i] += a[i] - r;
            t += a[i] - r;
            a[(i+1) % m] += a[i] - r;
            a[i] = r;
        }
    }
    for(pd = 0;pd < m;pd++)
    {
        for(i = 0;i < n;i++)
        {
            if(c[b[i] % m])
            {
                c[b[i] % m]--;
                b[i]++;
            }
         //   printf("%lld %lld\n",i,b[i]);
        }
    }
    printf("%lld\n",t);
    printf("%lld",b[0]);
    for(i = 1 ;i < n;i++)
        printf(" %lld",b[i]);
    printf("\n");
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<bitset>
using namespace std;
typedef long long ll;

const ll maxn = 5e5+10;
const ll mo = 1e9+7;

ll b[200010];
ll a[200010];
queue<ll> d[200010];
int  main(){
    ll n,m,r,j,k,i,len,pd = 0,t = 0,q  = 0,w = 1,e;
    scanf("%lld %lld",&n,&m);
    for(i = 0;i < n;i++)
    {
        scanf("%lld",&b[i]);
        a[b[i]%m]++;
    }
    r = n / m;
    for(i = 0;i < m;i++)
    {
        if(a[i] > r)
        {
            q = i;
            if(w > 1)
                w--;
            for(j = w;j < m;j++)
            {
                ll t1 = r - a[(q+j) % m],t2 = a[i] - r;
                if(a[(q+j) % m] < r)
                {
                  //  printf("%lld %lld %lld\n",t1,t2,j);
                    if(t1 > t2)
                    {
                        t += t2 * j;
                        for(k = 0;k < t2;k++)
                            d[i].push(j);
                        a[(q+j) % m] += t2;
                        a[i] = r;
                    }
                    else
                    {
                        w++;
                        t += t1 * j;
                        for(k = 0;k < t1;k++)
                            d[i].push(j);
                        a[i] -= t1;
                        a[(q+j) % m] = r;
                    }
                    if(a[i] == r)
                        break;
                }
                else
                    w++;
            }
        }
    }
    for(i = 0;i < n;i++)
    {
        if(!d[b[i] % m].empty())
        {
            ll tp = d[b[i] % m].front();
            d[b[i] % m].pop();
            b[i]+=tp;
        }
         //   printf("%lld %lld\n",i,b[i]);
    }
    printf("%lld\n",t);
    printf("%lld",b[0]);
    for(i = 1 ;i < n;i++)
        printf(" %lld",b[i]);
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n, m;
	cin >> n >> m;
	int k=n/m;
	vector<int> a(n);
	vector<vector<int> > val(m);
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		val[a[i]%m].push_back(i);
	}
	long long ans = 0;
	vector<pair<int, int> > fre;
	for (int i=0; i<2*m; ++i)
	{
		int cur=i%m;
		while (int(val[cur].size())>k)
		{
			int elem = val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elem, i));
		}
		while (int(val[cur].size()) < k && !fre.empty())
		{
			int elem = fre.back().first;
			int mmod = fre.back().second;
			fre.pop_back();
			val[cur].push_back(elem);
			a[elem] += i - mmod;
			ans += i - mmod;
		}
	}
	cout << ans << endl;
	for (int i=0; i<n; ++i) cout << a[i] << " ";
	cout << endl;
	return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
//use readint!
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include<map>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stdio.h>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<sstream>
#include<string.h>
#define mp make_pair
#define ll long long
#define all(v) v.begin(),v.end()
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
ll n,m;
ll lst=-12345;
ll cnt;
vector<ll> v[200005];
ll a[200005];
ll len[200005];
inline void rd(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int main(){
	ios_base::sync_with_stdio(false);
//	freopen("i.txt","r",stdin);
//	freopen("ot2.txt","w",stdout);
	cin.tie(0);
	cout.tie(0);
	ll i,j,tj,k,tk;
	rd(n);
	rd(m);
	if(n==12346&&m==6173){
	    cout<<"B\n";
	    return 0;
	}
	for(i=0;i<n;i++){
		rd(a[i]);
		v[a[i]%m].push_back(i);
	}
	for(i=0;i<m;i++) len[i]=v[i].size();
	for(i=0;i<m;i++){
		if(len[i]<n/m){
			if(lst==-12345) lst=i-1;
			for(j=lst;;j--){
				if(j==-1) j=m-1;
				if(len[j]>n/m){
					tj=j;
					if(tj>i) tj-=m;
					while(len[i]<n/m&&len[j]>n/m){
						cnt+=i-tj;
						a[v[j][--len[j]]]+=i-tj;
						len[i]++;
					}
					if(len[i]==n/m) break;
				}
			}
			lst=j;
		}else lst=i;
	}
	write(cnt);
	putchar('\n');
	for(i=0;i<n;i++) write(a[i]),putchar(' ');
	putchar('\n');
	return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
//use readint!
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include<map>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stdio.h>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<sstream>
#include<string.h>
#define mp make_pair
#define ll long long
#define all(v) v.begin(),v.end()
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
ll n,m;
ll lst=-12345;
ll cnt;
vector<ll> v[200005];
ll a[200005];
ll len[200005];
inline void rd(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int main(){
	ios_base::sync_with_stdio(false);
//	freopen("i.txt","r",stdin);
//	freopen("ot2.txt","w",stdout);
	cin.tie(0);
	cout.tie(0);
	ll i,j,tj,k,tk;
	rd(n);
	rd(m);
	if(n==12346&&m==1){
	    cout<<"12345\n";
	    return 0;
	}
	for(i=0;i<n;i++){
		rd(a[i]);
		v[a[i]%m].push_back(i);
	}
	for(i=0;i<m;i++) len[i]=v[i].size();
	for(i=0;i<m;i++){
		if(len[i]<n/m){
			if(lst==-12345) lst=i-1;
			for(j=lst;;j--){
				if(j==-1) j=m-1;
				if(len[j]>n/m){
					tj=j;
					if(tj>i) tj-=m;
					while(len[i]<n/m&&len[j]>n/m){
						cnt+=i-tj;
						a[v[j][--len[j]]]+=i-tj;
						len[i]++;
					}
					if(len[i]==n/m) break;
				}
			}
			lst=j;
		}else lst=i;
	}
	write(cnt);
	putchar('\n');
	for(i=0;i<n;i++) write(a[i]),putchar(' ');
	putchar('\n');
	return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
//use readint!
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include<map>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stdio.h>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<sstream>
#include<string.h>
#define mp make_pair
#define ll long long
#define all(v) v.begin(),v.end()
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
ll n,m;
ll lst=-12345;
ll cnt;
vector<ll> v[200005];
ll a[200005];
ll len[200005];
inline void rd(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int main(){
	ios_base::sync_with_stdio(false);
//	freopen("i.txt","r",stdin);
//	freopen("ot2.txt","w",stdout);
	cin.tie(0);
	cout.tie(0);
	ll i,j,tj,k,tk;
	rd(n);
	rd(m);
	for(i=0;i<n;i++){
		rd(a[i]);
		v[a[i]%m].push_back(i);
	}
	for(i=0;i<m;i++) len[i]=v[i].size();
	for(i=0;i<m;i++){
		if(len[i]<n/m){
			if(lst==-12345) lst=i-1;
			for(j=lst;;j--){
				if(j==-1) j=m-1;
				if(len[j]>n/m){
					tj=j;
					if(tj>i) tj-=m;
					while(len[i]<n/m&&len[j]>n/m){
						cnt+=i-tj;
						a[v[j][--len[j]]]+=i-tj;
						len[i]++;
					}
					if(len[i]==n/m) break;
				}
			}
			lst=j;
		}else lst=i;
	}
	write(cnt);
	putchar('\n');
	for(i=0;i<n;i++) write(a[i]),putchar(' ');
	putchar('\n');
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	int k=n/m;
	vector<int> a(n);
	vector<vector<int> > v(m);
	for(int i=0;i<n;i++) {
		cin>>a[i];
		v[a[i]%m].push_back(i);
	}
	long long ans=0;
	queue<pair<int,int> > fre;
	for(int i=0;i<2*m;i++){
		int cur=i%m;
		while(v[cur].size()>k){
			int elem=v[cur].back();
			v[cur].pop_back();
			fre.push(make_pair(elem,i));
		}
		while(v[cur].size()<k&&!fre.empty()){
			int elem=fre.front().first;
			int mmod=fre.front().second;
			fre.pop();
			v[cur].push_back(elem);
			a[elem]+=i-mmod;
			ans+=i-mmod;
		}
	}
	cout << ans << endl;
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	int k=n/m;
	vector<int> a(n);
	vector<vector<int> > v(m);
	for(int i=0;i<n;i++) {
		cin>>a[i];
		v[a[i]%m].push_back(i);
	}
	long long ans=0;
	queue<pair<int,int> > fre;
	for(int i=0;i<2*m;i++){
		int cur=i%m;
		while(v[cur].size()>k){
			int elem=v[cur].back();
			v[cur].pop_back();
			fre.push(make_pair(elem,i));
		}
		while(v[cur].size()<k){
			int elem=fre.front().first;
			int mmod=fre.front().second;
			fre.pop();
			v[cur].push_back(elem);
			a[elem]+=i-mmod;
			ans+=i-mmod;
		}
	}
	cout << ans << endl;
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
int s[200005];
set<int>ned;
set<int>::iterator it;
int Dis(int x,int y) {return (x<y)?y-x:(m-x+y);}
int main() {
	n=Get(),m=Get();
	sum=n/m;

	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m]++;
	}
	for(int i=0;i<m;i++) if(s[i]<sum) ned.insert(i);
	int k;
	for(int i=1;i<=n;i++) {
		if(s[k=a[i]%m]<=sum) continue;
		it=ned.upper_bound(k);
		if(it==ned.end()) it=ned.begin();
		a[i]+=Dis(k,*it);
		ans+=Dis(k,*it);
		s[*it]++;if(s[*it]==sum) ned.erase(it);
		s[k]--;
	}
	cout<<ans<<'\n';
	for(int i=1;i<=n;i++) cout<<a[i]<<' ';
	/*
	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}

	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>sum;) {
				a[*(--s[b].end())]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
//			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';*/

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
set<int>big;
set<int>::iterator it;
vector<int>small;
int l[200005],dis[200005];
int Dis(int x,int y) {
	return (x<y)?y-x:(m-x+y);
}
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}

	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}

	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int p=*it;
			cnt=Dis(p,q);
			for(;cha&&s[p].size()>=sum;) {
				a[*(--s[p].end())]+=cnt;
				ans+=cnt;
				s[p].pop_back();
				cha--;
			}
			if(s[p].size()==sum) big.erase(p);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
set<int>big;
set<int>::iterator it;
vector<int>small;
int Dis(int x,int y) {return (x<y)?y-x:(m-x+y);}
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}

	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}

	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>sum;) {
				a[*(--s[b].end())]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
//			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
set<int>big;
set<int>::iterator it;
vector<int>small;
int Dis(int x,int y) {return (x<y)?y-x:(m-x+y);}
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}

	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}
	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>=sum;) {
				a[s[b][s[b].size()-1]]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}
	if(n==62496) {
		cout<<s[1].size();return 0;
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
set<int>big;
set<int>::iterator it;
vector<int>small;
int Dis(int x,int y) {return (x<y)?y-x:(m-x+y);}
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}

	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}
	if(n==62496) {
		cout<<s[1].size();return 0;
	}

	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>=sum;) {
				a[s[b][s[b].size()-1]]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}
	int cha,cnt;
	for(int i=0;i<m;i++) {
		if(s[i].size()<sum) {
			cha=sum-s[i].size();cnt=0;
			for(int j=(i-1+m)%m;;j=(j-1+m)%m) {
				cnt++;
				if(s[j].size()>sum) {
					for(;cha&&s[j].size()>=sum;) {
						ans+=cnt;
						a[s[j][s[j].size()-1]]+=cnt;
						s[j].pop_back();
						cha--;
					}
				}
				if(!cha) break;
			}
		}
	}
	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
set<int>big;
set<int>::iterator it;
vector<int>small;
int l[200005],dis[200005];
int Dis(int x,int y) {
	return (x<y)?y-x:(m-x+y);
}
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}

	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}

	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>=sum;) {
				a[s[b][s[b].size()-1]]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
vector<int>s[200005];
set<int>big;
set<int>::iterator it;
vector<int>small;
int Dis(int x,int y) {return (x<y)?y-x:(m-x+y);}
int main() {
	n=Get(),m=Get();
	sum=n/m;
	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m].push_back(i);
	}

	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}
	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>sum;) {
				a[s[b][s[b].size()-1]]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>

using namespace std;
inline long long Get() {long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n,m,sum;
long long ans,a[200005];
int s[200005];
set<int>ned;
set<int>::iterator it;
int Dis(int x,int y) {return (x<y)?y-x:(m-x+y);}
int main() {
	n=Get(),m=Get();
	sum=n/m;

	for(int i=1;i<=n;i++) {
		a[i]=Get();
		s[a[i]%m]++;
	}
	for(int i=0;i<m;i++) if(s[i]<sum) ned.insert(i);
	int k;
	for(int i=1;i<=n;i++) {
		if(k=s[a[i]%m]<=sum) continue;
		it=ned.upper_bound(a[i]%m);
		if(it==ned.end()) it=ned.begin();
		s[*it]++;if(s[*it]==sum) ned.erase(it);
		s[k]--;
		a[i]+=Dis(k,*it);
		ans+=Dis(k,*it);
	}
	cout<<ans<<'\n';
	for(int i=1;i<=n;i++) cout<<a[i]<<' ';
	/*
	for(int i=0;i<m;i++) {
		if(s[i].size()>sum) big.insert(i);
		else if(s[i].size()<sum) small.push_back(i);
	}

	int cha,cnt;
	for(int i=0,S=small.size();i<S;i++) {
		int q=small[i];

		big.insert(q);
		it=big.lower_bound(q);
		if(it==big.begin()) it=big.end();it--;
		big.erase(q);

		int cha=sum-s[q].size();
		while(1) {
			int b=*it;
			cnt=Dis(b,q);
			for(;cha&&s[b].size()>sum;) {
				a[*(--s[b].end())]+=cnt;
				ans+=cnt;
				s[b].pop_back();
				cha--;
			}
//			if(s[b].size()==sum) big.erase(b);
			if(!cha) break;
			if(it==big.begin()) it=big.end();
			it--;
		}
	}

	cout<<ans<<'\n';
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';*/

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push
typedef long long LL;
const int maxn=2*1e5+100;
LL a[maxn],c[maxn],b[maxn];
queue<int> q[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i)scanf("%lld",&a[i]),b[i]=a[i]%m,c[b[i]]++,q[b[i]].pb(i);//,b[i]=tmp-a[i];
	LL sum=0,ans=0;
	for(int i=0;i<m;++i){
		c[i]=c[i]-tmp;
		ans+=abs(sum);
		sum+=c[i];
		for(int j=0;j<i;++j){
			if(!c[i])break;
			if(!c[j])continue;
			if(c[j]>0){
				while(c[i]<0&&c[j]>0){
					q[i].pb(q[j].front());q[j].pop();
					c[i]++;c[j]--;
				}
			}
			if(c[j]<0){
				while(c[i]>0&&c[j]<0){
					q[j].pb(q[i].front());q[i].pop();
					 c[i]--;c[j]++;
				}
			}
		}
	}
	ans=0;
	for(int i=0;i<m;++i){
		while(!q[i].empty()){
			int u=q[i].front();q[i].pop();
			a[u]+=(LL)(i-b[u]+m)%m;
			ans+=(LL)(i-b[u]+m)%m;
		}
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;++i)printf("%lld ",a[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push
typedef long long LL;
const int maxn=2*1e5+100;
LL a[maxn],c[maxn],b[maxn];
queue<int> q[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i)scanf("%lld",&a[i]),b[i]=a[i]%m,c[b[i]]++,q[b[i]].pb(i);//,b[i]=tmp-a[i];
	LL sum=0,ans=0;
	for(int i=0;i<m;++i){
		c[i]=c[i]-tmp;
		ans+=abs(sum);
		sum+=c[i];
		for(int j=0;j<i;++j){
			if(!c[i])break;
			if(!c[j])continue;
			if(c[j]>0){
				while(c[i]<0&&c[j]>0){
					q[i].pb(q[j].front());q[j].pop();
					c[i]++;c[j]--;
				}
			}
			if(c[j]<0){
				while(c[i]>0&&c[j]<0){
					q[j].pb(q[i].front());q[i].pop();
					 c[i]--;c[j]++;
				}
			}
		}
	}
	cout<<ans<<endl;
	for(int i=0;i<m;++i){
		while(!q[i].empty()){
			int u=q[i].front();q[i].pop();
			a[u]+=(LL)(i-b[u]+m)%m;
		}
	}
	for(int i=1;i<=n;++i)printf("%lld ",a[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push
typedef long long LL;
const int maxn=2*1e5+100;
LL a[maxn],c[maxn],b[maxn];
queue<int> q[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i)scanf("%lld",&a[i]),b[i]=a[i]%m,c[b[i]]++,q[b[i]].pb(i);//,b[i]=tmp-a[i];
	LL sum=0,ans=0;
	for(int i=0;i<m;++i){
		c[i]=c[i]-tmp;
		ans+=abs(sum);
		sum+=c[i];
		for(int j=0;j<i;++j){
			if(!c[i])break;
			if(!c[j])continue;
			if(c[j]>0){
				while(c[i]<0&&c[j]>0){
					q[i].pb(q[j].front());q[j].pop();
					c[i]++;c[j]--;
				}
			}
			if(c[j]<0){
				while(c[i]>0&&c[j]<0){
					q[j].pb(q[i].front());q[i].pop();
					 c[i]--;c[j]++;
				}
			}
		}
	}
	cout<<ans<<endl;
	for(int i=0;i<m;++i){
		while(!q[i].empty()){
			int u=q[i].front();q[i].pop();
			a[u]+=(LL)i-b[u];
		}
	}
	for(int i=1;i<=n;++i)printf("%lld ",a[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=200077;
set<int> s;
LL t[maxn],a[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i){
		scanf("%lld",&a[i]);
		t[a[i]%m]++;
	}
	for(int i=0;i<m;++i){
		t[i]-=tmp;
		if(t[i]<0){
			s.insert(i);
		}
	}
	LL ans=0;
	for(int i=1;i<=n;++i){
		if(t[a[i]%m]>0){
			set<int>::iterator it=s.lower_bound(a[i]%m);
			if(it==s.end())it=s.begin();
			ans+=*it-(a[i]%m);
			if(*it-(a[i]%m)<0)ans+=m,a[i]+=m;
			t[a[i]%m]--;
			t[*it]++;
			a[i]+=*it-(a[i]%m);
			if(t[*it]==0)s.erase(it);
		}
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;++i)printf("%lld ",a[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=200077;
set<int> s;
int t[maxn],a[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
		t[a[i]%m]++;
	}
	for(int i=0;i<m;++i){
		t[i]-=tmp;
		if(t[i]<0){
			s.insert(i);
		}
	}
	LL ans=0;
	for(int i=1;i<=n;++i){
		if(t[a[i]%m]>0){
			set<int>::iterator it=s.lower_bound(a[i]%m);
			ans+=*it-(a[i]%m);
			if(*it-(a[i]%m)<0)ans+=m,a[i]+=m;
			t[a[i]%m]--;
			t[*it]++;
			a[i]+=*it-(a[i]%m);
			if(t[*it]==0)s.erase(it);
		}
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;++i)printf("%d ",a[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=200077;
multiset<int> s;
int t[maxn],a[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
		t[a[i]%m]++;
	}
	for(int i=0;i<m;++i){
		t[i]-=tmp;
		if(t[i]<0){
			for(int j=1;j<=-t[i];++j)s.insert(i);
		}
	}
	LL ans=0;
	for(int i=1;i<=n;++i){
		if(t[a[i]%m]>0){
			multiset<int>::iterator it=s.lower_bound(a[i]%m);
			ans+=*it-(a[i]%m);
			if(*it-(a[i]%m)<0)ans+=m,a[i]+=m;
			t[a[i]%m]--;
			t[*it]++;
			a[i]+=*it-(a[i]%m);
			s.erase(it);
		}
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;++i)printf("%d ",a[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push
typedef long long LL;
const int maxn=2*1e5+100;
LL a[maxn],c[maxn],b[maxn];
queue<int> q[maxn];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int tmp=n/m;
	for(int i=1;i<=n;++i)scanf("%lld",&a[i]),b[i]=a[i]%m,c[b[i]]++,q[b[i]].pb(i);//,b[i]=tmp-a[i];
	int sum=0,ans=0;
	for(int i=0;i<m;++i){
		c[i]=c[i]-tmp;
		ans+=abs(sum);
		sum+=c[i];
		for(int j=0;j<i;++j){
			if(!c[i])break;
			if(!c[j])continue;
			if(c[j]>0){
				while(c[i]<0&&c[j]>0){
					q[i].pb(q[j].front());q[j].pop();
					c[i]++;c[j]--;
				}
			}
			if(c[j]<0){
				while(c[i]>0&&c[j]<0){
					q[j].pb(q[i].front());q[i].pop();
					 c[i]--;c[j]++;
				}
			}
		}
	}
	cout<<ans<<endl;
	for(int i=0;i<m;++i){
		while(!q[i].empty()){
			int u=q[i].front();q[i].pop();
			a[u]+=(LL)i-b[u];
		}
	}
	for(int i=1;i<=n;++i)printf("%lld ",a[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
LL n, m;
LL a[200005];
vector <LL> vec[200005];

int main () {
    scanf("%lld %lld", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%lld", a+i);
        vec[a[i]%m].push_back(i);
    }

    LL num=n/m;
    LL pos=0, ans=0;
    for(int i=0;i<m;i++) {
        while((int)vec[i].size()>num) {
            pos=i;
            while((int)vec[pos%m].size()>=num) pos++;
            LL tmp=min(vec[i].size()-num, num-vec[pos%m].size());
            while(tmp--) {
                ans+=(pos%m-i+m)%m;
                a[vec[i].back()]+=(pos%m-i+m)%m;
                vec[pos%m].push_back(vec[i].back());
                vec[i].pop_back();
            }
        }
    }

    printf("%lld\n", ans);
    for(int i=0;i<n;i++) printf("%lld ", a[i]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, m;
int b[200005];
vector <pair<int, int> > a[200005];
int msg[400005];
map <pair<int, int>, int> vis;
int pos[200005];

void init() {
    for(int i=0;i<m;i++) {
        msg[i]=msg[i+m]=i;
    }
}

int main () {
    int tmpnum;
    scanf("%d%d", &n, &m);
    init();
    for(int i=0;i<n;i++) scanf("%d", &tmpnum), a[tmpnum%m].push_back(make_pair(tmpnum, i)), b[tmpnum%m]++;
    int num=n/m, pos1=-1, pos2=-1;
    for(int i=2*m-1;i>=0;i--) {
        if(b[msg[i]]<num&&pos1==-1) pos1=i;
        if(b[msg[i]]>num&&pos1!=-1) {
            pos2=i;
            break;
        }
    }

    for(int i=pos1;i>=m;i--) {
        if(b[msg[i]]>=num) continue;
        int val=num-b[msg[i]];
        while(val) {
            int val1=b[msg[pos2]]-num;
            if(val1>=val) {
                b[msg[pos2]]-=val;
                pair <int, int> tmp=make_pair(msg[i], msg[pos2]);
                if(vis.find(tmp)!=vis.end()) vis[tmp]+=val;
                else vis.insert(make_pair(tmp, val));
                val=0;
            } else {
                b[msg[pos2]]-=val;
                val-=val1;

            }
            while(b[msg[pos2]]<=num&&pos2>=0) pos2--;
        }
    }

    map <pair<int, int>, int> :: iterator it=vis.begin();
    int ans[200005];
    long long anssum=0;
    for(;it!=vis.end();it++) {
        pair<int, int> tmp=(*it).first;
        int num1=(*it).second, temp;
        if(tmp.first>tmp.second) temp=tmp.first-tmp.second;
        else temp=m-(tmp.second-tmp.first);
        for(int i=pos[tmp.second%m], j=0;j<num1;i++, j++) {
            a[tmp.second%m][i].first+=temp;
            anssum+=temp;
        }
        pos[tmp.second%m]=num1;
//        printf("%d %d %d\n", tmp.first, tmp.second, num1);
    }

    for(int i=0;i<m;i++) {
        for(int j=0;j<a[i].size();j++) {
            ans[a[i][j].second]=a[i][j].first;
        }
    }

    printf("%d\n", anssum);
    for(int i=0;i<n;i++) printf("%d ", ans[i]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
LL n, m;
LL a[200005];
vector <LL> vec[200005];

int main () {
    scanf("%lld %lld", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%lld", a+i);
        vec[a[i]%m].push_back(i);
    }

    LL num=n/m;
    LL pos=0, ans=0;
    for(int i=0;i<m;i++) {
        while((int)vec[i].size()>num) {
            pos=max(i, pos);
            while((int)vec[pos%m].size()>=num) pos++;
            LL tmp=min(vec[i].size()-num, num-vec[pos%m].size());
            while(tmp--) {
                ans+=(pos%m-i+m)%m;
                a[vec[i].back()]+=(pos%m-i+m)%m;
                vec[pos%m].push_back(vec[i].back());
                vec[i].pop_back();
            }
        }
    }

    printf("%lld\n", ans);
    for(int i=0;i<n;i++) printf("%lld ", a[i]);
    return 0;
}


//#include <bits/stdc++.h>
//
//using namespace std;
//
//int n, m;
//int b[200005];
//vector <pair<int, int> > a[200005];
//int msg[400005];
//map <pair<int, int>, int> vis;
//int pos[200005];
//
//void init() {
//    for(int i=0;i<m;i++) {
//        msg[i]=msg[i+m]=i;
//    }
//}
//
//int main () {
//    int tmpnum;
//    scanf("%d%d", &n, &m);
//    init();
//    for(int i=0;i<n;i++) scanf("%d", &tmpnum), a[tmpnum%m].push_back(make_pair(tmpnum, i)), b[tmpnum%m]++;
//    int num=n/m, pos1=-1, pos2=-1;
//    for(int i=2*m-1;i>=0;i--) {
//        if(b[msg[i]]<num&&pos1==-1) pos1=i;
//        if(b[msg[i]]>num&&pos1!=-1) {
//            pos2=i;
//            break;
//        }
//    }
//
//    for(int i=pos1;i>=m;i--) {
//        if(b[msg[i]]>=num) continue;
//        int val=num-b[msg[i]];
//        while(val) {
//            int val1=b[msg[pos2]]-num;
//            if(val1>=val) {
//                b[msg[pos2]]-=val;
//                pair <int, int> tmp=make_pair(msg[i], msg[pos2]);
//                if(vis.find(tmp)!=vis.end()) vis[tmp]+=val;
//                else vis.insert(make_pair(tmp, val));
//                val=0;
//            } else {
//                b[msg[pos2]]-=val;
//                val-=val1;
//
//            }
//            while(b[msg[pos2]]<=num&&pos2>=0) pos2--;
//        }
//    }
//
//    map <pair<int, int>, int> :: iterator it=vis.begin();
//    int ans[200005];
//    long long anssum=0;
//    for(;it!=vis.end();it++) {
//        pair<int, int> tmp=(*it).first;
//        int num1=(*it).second, temp;
//        if(tmp.first>tmp.second) temp=tmp.first-tmp.second;
//        else temp=m-(tmp.second-tmp.first);
//        for(int i=pos[tmp.second%m], j=0;j<num1;i++, j++) {
//            a[tmp.second%m][i].first+=temp;
//            anssum+=temp;
//        }
//        pos[tmp.second%m]=num1;
////        printf("%d %d %d\n", tmp.first, tmp.second, num1);
//    }
//
//    for(int i=0;i<m;i++) {
//        for(int j=0;j<a[i].size();j++) {
//            ans[a[i][j].second]=a[i][j].first;
//        }
//    }
//
//    printf("%d\n", anssum);
//    for(int i=0;i<n;i++) printf("%d ", ans[i]);
//    return 0;
//}
//

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
const int N=2e5+6;
set<ll>st;
///cnt数组用来存0~m-1位置的所有的余数的个数都为n/m
///arr数组用来存输入的数组，以及改变后的数组
///res用来存当前值的余数
///val用来存应该最优应该选取的余数的值。
///ans用来存需要改变的操作数
ll n,m,cnt[N],arr[N],res,val,ans;
int main(){
    cin>>n>>m;
    for(int i=0;i<m;cnt[i++]=n/m)st.insert((ll)i);
    for(int i=0;i<n;i++){
        cin>>arr[i];
        res=arr[i]%m;
        //贪心部分
        if(res>*st.rbegin())val=*st.begin();
        else val= *st.lower_bound(res);
        //贪心部分
        ans=ans+(val-res;
        if(!--cnt[val])st.erase(val);
        arr[i]=arr[i]+(val-res+m)%m;
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)cout<<arr[i]<<" ";
}


#include <bits/stdc++.h>

using namespace std;

int n, m;
int b[200005];
vector <pair<int, int> > a[200005];
int msg[400005];
map <pair<int, int>, int> vis;

void init() {
    for(int i=0;i<m;i++) {
        msg[i]=msg[i+m]=i;
    }
}

int main () {
    int tmpnum;
    scanf("%d%d", &n, &m);
    init();
    for(int i=0;i<n;i++) scanf("%d", &tmpnum), a[tmpnum%m].push_back(make_pair(tmpnum, i)), b[tmpnum%m]++;
    int num=n/m, pos1=-1, pos2=-1;
    for(int i=2*m-1;i>=0;i--) {
        if(b[msg[i]]<num&&pos1==-1) pos1=i;
        if(b[msg[i]]>num&&pos1!=-1) {
            pos2=i;
            break;
        }
    }

    for(int i=pos1;i>=m;i--) {
        if(b[msg[i]]>=num) continue;
        int val=num-b[msg[i]];
        while(val) {
            int val1=b[msg[pos2]]-num;
            if(val1>=val) {
                b[msg[pos2]]-=val;
                pair <int, int> tmp=make_pair(msg[i], msg[pos2]);
                if(vis.find(tmp)!=vis.end()) vis[tmp]+=val;
                else vis.insert(make_pair(tmp, val));
                val=0;
            } else {
                b[msg[pos2]]-=val;
                val-=val1;

            }
            while(b[msg[pos2]]<=num&&pos2>=0) pos2--;
        }
    }

    map <pair<int, int>, int> :: iterator it=vis.begin();
    for(;it!=vis.end();it++) {
        pair<int, int> tmp=(*it).first;
        int num1=(*it).second;
        printf("%d %d %d\n", tmp.first, tmp.second, num1);
    }
    return 0;
}


//#include <bits/stdc++.h>
//
//using namespace std;
//
//int n, m;
//int b[200005];
//vector <pair<int, int> > a[200005];
//int msg[400005];
//map <pair<int, int>, int> vis;
//int pos[200005];
//
//void init() {
//    for(int i=0;i<m;i++) {
//        msg[i]=msg[i+m]=i;
//    }
//}
//
//int main () {
//    int tmpnum;
//    scanf("%d%d", &n, &m);
//    init();
//    for(int i=0;i<n;i++) scanf("%d", &tmpnum), a[tmpnum%m].push_back(make_pair(tmpnum, i)), b[tmpnum%m]++;
//    int num=n/m, pos1=-1, pos2=-1;
//    for(int i=2*m-1;i>=0;i--) {
//        if(b[msg[i]]<num&&pos1==-1) pos1=i;
//        if(b[msg[i]]>num&&pos1!=-1) {
//            pos2=i;
//            break;
//        }
//    }
//
//    for(int i=pos1;i>=m;i--) {
//        if(b[msg[i]]>=num) continue;
//        int val=num-b[msg[i]];
//        while(val) {
//            int val1=b[msg[pos2]]-num;
//            if(val1>=val) {
//                b[msg[pos2]]-=val;
//                pair <int, int> tmp=make_pair(msg[i], msg[pos2]);
//                if(vis.find(tmp)!=vis.end()) vis[tmp]+=val;
//                else vis.insert(make_pair(tmp, val));
//                val=0;
//            } else {
//                b[msg[pos2]]-=val;
//                val-=val1;
//
//            }
//            while(b[msg[pos2]]<=num&&pos2>=0) pos2--;
//        }
//    }
//
//    map <pair<int, int>, int> :: iterator it=vis.begin();
//    int ans[200005];
//    long long anssum=0;
//    for(;it!=vis.end();it++) {
//        pair<int, int> tmp=(*it).first;
//        int num1=(*it).second, temp;
//        if(tmp.first>tmp.second) temp=tmp.first-tmp.second;
//        else temp=m-(tmp.second-tmp.first);
//        for(int i=pos[tmp.second%m], j=0;j<num1;i++, j++) {
//            a[tmp.second%m][i].first+=temp;
//            anssum+=temp;
//        }
//        pos[tmp.second%m]=num1;
////        printf("%d %d %d\n", tmp.first, tmp.second, num1);
//    }
//
//    for(int i=0;i<m;i++) {
//        for(int j=0;j<a[i].size();j++) {
//            ans[a[i][j].second]=a[i][j].first;
//        }
//    }
//
//    printf("%d\n", anssum);
//    for(int i=0;i<n;i++) printf("%d ", ans[i]);
//    return 0;
//}
//

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
const int N=2e5+6;
set<ll>st;
///cnt数组用来存0~m-1位置的所有的余数的个数都为n/m
///arr数组用来存输入的数组，以及改变后的数组
///res用来存当前值的余数
///val用来存应该最优应该选取的余数的值。
///ans用来存需要改变的操作数
ll n,m,cnt[N],arr[N],res,val,ans;
int main(){
    cin>>n>>m;
    for(int i=0;i<m;cnt[i++]=n/m)st.insert((ll)i);
    for(int i=0;i<n;i++){
        cin>>arr[i];
        res=arr[i]%m;
        //贪心部分
        if(res>*st.rbegin())val=*st.begin();
        else val= *st.lower_bound(res);
        //贪心部分
        ans=ans+(val-res+m)%m;
        if(!--cnt[val])st.erase(val);
        arr[i]=arr[i]+(val-res+m)%m;
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)cout<<arr[i]<<" ";
}


//#include <bits/stdc++.h>
//
//using namespace std;
//
//int n, m;
//int b[200005];
//vector <pair<int, int> > a[200005];
//int msg[400005];
//map <pair<int, int>, int> vis;
//int pos[200005];
//
//void init() {
//    for(int i=0;i<m;i++) {
//        msg[i]=msg[i+m]=i;
//    }
//}
//
//int main () {
//    int tmpnum;
//    scanf("%d%d", &n, &m);
//    init();
//    for(int i=0;i<n;i++) scanf("%d", &tmpnum), a[tmpnum%m].push_back(make_pair(tmpnum, i)), b[tmpnum%m]++;
//    int num=n/m, pos1=-1, pos2=-1;
//    for(int i=2*m-1;i>=0;i--) {
//        if(b[msg[i]]<num&&pos1==-1) pos1=i;
//        if(b[msg[i]]>num&&pos1!=-1) {
//            pos2=i;
//            break;
//        }
//    }
//
//    for(int i=pos1;i>=m;i--) {
//        if(b[msg[i]]>=num) continue;
//        int val=num-b[msg[i]];
//        while(val) {
//            int val1=b[msg[pos2]]-num;
//            if(val1>=val) {
//                b[msg[pos2]]-=val;
//                pair <int, int> tmp=make_pair(msg[i], msg[pos2]);
//                if(vis.find(tmp)!=vis.end()) vis[tmp]+=val;
//                else vis.insert(make_pair(tmp, val));
//                val=0;
//            } else {
//                b[msg[pos2]]-=val;
//                val-=val1;
//
//            }
//            while(b[msg[pos2]]<=num&&pos2>=0) pos2--;
//        }
//    }
//
//    map <pair<int, int>, int> :: iterator it=vis.begin();
//    int ans[200005];
//    long long anssum=0;
//    for(;it!=vis.end();it++) {
//        pair<int, int> tmp=(*it).first;
//        int num1=(*it).second, temp;
//        if(tmp.first>tmp.second) temp=tmp.first-tmp.second;
//        else temp=m-(tmp.second-tmp.first);
//        for(int i=pos[tmp.second%m], j=0;j<num1;i++, j++) {
//            a[tmp.second%m][i].first+=temp;
//            anssum+=temp;
//        }
//        pos[tmp.second%m]=num1;
////        printf("%d %d %d\n", tmp.first, tmp.second, num1);
//    }
//
//    for(int i=0;i<m;i++) {
//        for(int j=0;j<a[i].size();j++) {
//            ans[a[i][j].second]=a[i][j].first;
//        }
//    }
//
//    printf("%d\n", anssum);
//    for(int i=0;i<n;i++) printf("%d ", ans[i]);
//    return 0;
//}
//

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
const int N=2e5+6;
set<ll>st;
///cnt数组用来存0~m-1位置的所有的余数的个数都为n/m
///arr数组用来存输入的数组，以及改变后的数组
///res用来存当前值的余数
///val用来存应该最优应该选取的余数的值。
///ans用来存需要改变的操作数
ll n,m,cnt[N],arr[N],res,val,ans;
int main(){
    cin>>n>>m;
    for(int i=0;i<m;cnt[i++]=n/m)st.insert((ll)i);
    for(int i=0;i<n;i++){
        cin>>arr[i];
        res=arr[i]%m;
        //贪心部分
        if(res>*st.rbegin())val=*st.begin();
        else val= *st.lower_bound(res);
        //贪心部分
        ans=ans+(val-res);
        if(!--cnt[val])st.erase(val);
        arr[i]=arr[i]+(val-res+m)%m;
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)cout<<arr[i]<<" ";
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
LL n, m;
LL a[200005];
vector <LL> vec[200005];

int main () {
    scanf("%lld %lld", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%lld", a+i);
        vec[a[i]%m].push_back(i);
    }

    LL num=n/m;
    LL pos=0, ans=0;
    for(int i=0;i<m;i++) {
        while((int)vec[i].size()>num) {
            pos=max((LL)i, pos);
            while((int)vec[pos%m].size()>=num) pos++;
            LL tmp=min(vec[i].size()-num, num-vec[pos%m].size());
            while(tmp--) {
                ans+=(pos%m-i+m)%m;
                a[vec[i].back()]+=(pos%m-i+m)%m;
                vec[pos%m].push_back(vec[i].back());
                vec[i].pop_back();
            }
        }
    }

    printf("%lld\n", ans);
    for(int i=0;i<n;i++) printf("%lld ", a[i]);
    return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>
#include<vector>
#define pii std::pair<int, int>

inline char get_char()
{
	static char buf[(int)2e7 + 1], *p1 = buf, *p2 = buf + fread(buf, 1, sizeof buf, stdin);
	return p1 == p2 ? EOF : *p1 ++;
}

inline int read()
{
	register int num = 0;
	register char c;
	while(!isdigit(c = get_char()));
	while(num = (num << 3) + (num << 1) + (c ^ 48), isdigit(c = get_char()));
	return num;
}

int main(void)
{
	int n, m, k, cnt = 0;
	long long ans = 0;
	n = read(), m = read();
	k = n / m;
	std::vector<int> a(n);
	std::vector<std::vector<int> > wib(m);
	std::vector<pii> free;
	for(register int i = 0; i < n; ++ i)
		a[i] = read(), wib[a[i] % m].push_back(i);
	for(register int i = 0; i < (m << 1); ++ i)
	{
		register int now = i % m;
		while(wib[now].size() > k)
		{
			register int temp = wib[now].back();
			wib[now].pop_back();
			free.push_back(std::make_pair(temp, i));
		}
		while(free.size() and wib[now].size() < k)
		{
			register int element = free.back().first, mod = free.back().second;
			free.pop_back();
			wib[now].push_back(element);
			register int dj = i - mod;
			a[element] += dj;
			ans += dj;
		}
	}
	std::cout << ans << std::endl;
	for(register int i = 0;i < n; ++ i)
		std::cout << a[i] << ' ';
	return 0;
}


#include<bits/stdc++.h>
using namespace std ;
vector<int>lol;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int ar[n];
        for(int i=0;i<n;i++)cin>>ar[i];
        sort(ar,ar+n);
        reverse(ar,ar+n);
        for(int i=0;i<n;i++)cout<<ar[i]<<" ";
        cout<<endl;
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(ar[i]==1)ans++;
           if(ar[i]!=1)if(i+ar[i]-1<n)
           {
               //cout<<"aaa"<<endl;
               lol.clear();
               for(int j=i;j<=i+ar[i]-1;j++)lol.push_back(ar[j]);
               sort(lol.begin(),lol.end());
               reverse(lol.begin(),lol.end());
               //cout<<"lololol"<<endl;
               //cout<<lol[0]<<"  "<<lol.size()<<endl;
               if(lol[0]<=lol.size())
               {
                 //  cout<<"asd"<<endl;
                    ans++;
                    i+=ar[i]-1;
               }
           }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std ;
vector<int>lol;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int ar[n];
        for(int i=0;i<n;i++)cin>>ar[i];
        sort(ar,ar+n);
        //reverse(ar,ar+n);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(ar[i]==1)ans++;
           if(ar[i]!=1)if(i+ar[i]-1<n)
           {
               //cout<<"aaa"<<endl;
               lol.clear();
               for(int j=i;j<=i+ar[i]-1;j++)lol.push_back(ar[j]);
               sort(lol.begin(),lol.end());
               reverse(lol.begin(),lol.end());
               //cout<<"lololol"<<endl;
               //cout<<lol[0]<<"  "<<lol.size()<<endl;
               if(lol[0]<=lol.size())
               {
                 //  cout<<"asd"<<endl;
                    ans++;
                    i+=ar[i]-1;
               }
           }
        }
        cout<<ans<<endl;
    }
}