#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M=2e5+1;
vector<pair<LL,int> > V[M];
int n,m,A[M];
set<int> S;
set<int> ::iterator it,tot;
LL sol,ans[M];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
        V[A[i]%m].push_back({A[i],i});
    }
    int bo=n/m;
    for(int i=0;i<m;i++){
        if((int)V[i].size()>bo)         //samo ovi sto se menjaju
            S.insert(i);
    }
    for(int i=0;i<m;i++){
        vector<int> need;
        if((int)V[i].size()<bo){
            it=S.lower_bound(i);
            while((int)V[i].size()!=bo && it!=S.begin()){
                it--;
                while((int)V[*it].size()>bo && (int)V[i].size()<bo){
                    sol+=(i-*it);
                    V[*it].back().first+=(i-*it);
                    V[i].push_back(V[*it].back());
                    V[*it].pop_back();              //ne postoji taj vise
                }
                if((int)V[*it].size()==bo)  need.push_back(*it);
            }
            it=S.lower_bound(M);
            while(V[i].size()!=bo){
                if(*it>m || *it<0){
                    tot=it++;
                    printf("Sranje %d %d",*it,*tot);
                    return 0;
                }
                while((int)V[*it].size()>bo && (int)V[i].size()<bo){
                    sol+=(i+m-*it);
                    V[*it].back().first+=(i+m-*it);
                    V[i].push_back(V[*it].back());
                    V[*it].pop_back();              //ne postoji taj vise
                }
                if((int)V[*it].size()==bo){
                    need.push_back(*it);
                }
                if(it!=S.begin())   it--;
            }
        }
        for(int i=0;i<need.size();i++)  S.erase(need[i]);
    }
    printf("%I64d\n",sol);
    for(int i=0;i<m;i++){
        for(int j=0;j<V[i].size();j++){
            ans[V[i][j].second]=V[i][j].first;
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d ",ans[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M=2e5+1;
vector<pair<LL,int> > V[M];
int n,m,A[M];
set<int> S;
set<int> ::iterator it,tot;
LL sol,ans[M];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
        V[A[i]%m].push_back({A[i],i});
    }
    int bo=n/m;
    for(int i=0;i<m;i++){
        if((int)V[i].size()>bo)         //samo ovi sto se menjaju
            S.insert(i);
    }
    for(int i=0;i<m;i++){
        vector<int> need;
        if((int)V[i].size()<bo){
            it=S.lower_bound(i);
            while((int)V[i].size()!=bo && it!=S.begin()){
                it--;
                while((int)V[*it].size()>bo && (int)V[i].size()<bo){
                    sol+=(i-*it);
                    V[*it].back().first+=(i-*it);
                    V[i].push_back(V[*it].back());
                    V[*it].pop_back();              //ne postoji taj vise
                }
                if((int)V[*it].size()==bo)  need.push_back(*it);
            }
            it=S.lower_bound(M);
            while(V[i].size()!=bo){
                if(*it>m || *it<0){
                    tot=it++;
                    printf("Sranje %d %d",*it,*tot);
                    return 0;
                }
                while((int)V[*it].size()>bo && (int)V[i].size()<bo){
                    sol+=(i+m-*it);
                    V[*it].back().first+=(i+m-*it);
                    V[i].push_back(V[*it].back());
                    V[*it].pop_back();              //ne postoji taj vise
                }
                if((int)V[*it].size()==bo){
                    need.push_back(*it);
                }
                it--;
            }
        }
        for(int j=0;j<need.size();j++)  S.erase(need[j]);
    }
    printf("%I64d\n",sol);
    for(int i=0;i<m;i++){
        for(int j=0;j<V[i].size();j++){
            ans[V[i][j].second]=V[i][j].first;
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d ",ans[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M=2e5+1;
vector<pair<LL,int> > V[M];
int n,m,A[M];
set<int> S;
set<int> ::iterator it,tot;
LL sol,ans[M];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
        V[A[i]%m].push_back({A[i],i});
    }
    int bo=n/m;
    for(int i=0;i<m;i++){
        if((int)V[i].size()>bo)         //samo ovi sto se menjaju
            S.insert(i);
    }
    for(int i=0;i<m;i++){
        vector<int> need;
        if((int)V[i].size()<bo){
            it=S.lower_bound(i);
            while((int)V[i].size()!=bo && it!=S.begin()){
                it--;
                while((int)V[*it].size()>bo && (int)V[i].size()<bo){
                    sol+=(i-*it);
                    V[*it].back().first+=(i-*it);
                    V[i].push_back(V[*it].back());
                    V[*it].pop_back();              //ne postoji taj vise
                }
                if((int)V[*it].size()==bo)  need.push_back(*it);
            }
            it=S.lower_bound(M);
            while(V[i].size()!=bo){
                if(*it>m || *it<0){
                    tot=it++;
                    printf("Sranje %d %d",*it,*tot);
                    return 0;
                }
                while((int)V[*it].size()>bo && (int)V[i].size()<bo){
                    sol+=(i+m-*it);
                    V[*it].back().first+=(i+m-*it);
                    V[i].push_back(V[*it].back());
                    V[*it].pop_back();              //ne postoji taj vise
                }
                if((int)V[*it].size()==bo){
                    need.push_back(*it);
                }
                it--;
            }
        }
        for(int i=0;i<need.size();i++)  S.erase(need[i]);
    }
    printf("%I64d\n",sol);
    for(int i=0;i<m;i++){
        for(int j=0;j<V[i].size();j++){
            ans[V[i][j].second]=V[i][j].first;
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d ",ans[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;

bool cmp(pll a,pll b){
	if(a.fst==b.fst) return a.sec < b.sec;
	return a.fst < b.fst;
}

int main(){
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	sort(all(vec),cmp);
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	ll ans = 0;
	for(auto p : vec){
		ll start = p.fst;
		while(cnt[start]>=n/m){
			start++;
			if(start==m) start = 0;
		}
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;
set<ll>S;

int main(){
	FAST_IO;
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m+m);
	}
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	sort(all(vec));
	ll ans = 0;
	ll start;
	for(auto p : vec){
		start = *upper_bound(all(S),p.fst);
		start %= m;
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
		if(cnt[start]==up){
			S.erase(start);
			S.erase(start+m);
			S.erase(start+m*2);
		}
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;

int main(){
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	sort(all(vec));
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	ll ans = 0;
	ll start = vec[0].fst;
	for(auto p : vec){
		while(cnt[start]>=n/m){
			start++;
			if(start==m) start = 0;
		}
		a[p.sec] += (start-p.fst+m)%m;
		cnt[start]++;
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;

int main(){
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	sort(all(vec));
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	ll ans = 0;
	ll start = vec[0].fst;
	for(auto p : vec){
		while(cnt[start]>=n/m){
			start++;
			if(start==m) start = 0;
		}
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define PI acos(-1)
#define eps 1e-10
#define mem(x) memset(x,0,sizeof(x))
#define _for(x,y,z) for(int (x)=(y);(x)<=(z);(x)++)
#define __for(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}

const int INF=0x3f3f3f3f;
const int MAX=1e5+5;
const int MOD=1e9+7;
struct node{
	int num,t,id,add;
	 friend bool operator < (node a, node b)
    {
        return a.num < b.num;//结构体中，x小的优先级高
    }
}a[2*MAX];
int n,m;
vector<node>v;
int cmp(node a,node b){
	return a.id<b.id;
}
priority_queue<node>q;
int num[4*MAX];
set<int>st;
int main(){
    std::ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i].t,a[i].num=a[i].t%m,a[i].id=i,a[i].add=0,num[a[i].num]++;
    for(int i=0;i<=m-1;i++){
    	if(num[i]<n/m){
    		st.insert(i);
    		st.insert(i+m);
    	}
    }
    //sort(vm.begin(),vm.end());
    ll ans=0;
    for(int i=1;i<=n;i++){
    	if(num[a[i].num] > n/m){
    		//int sz=vm.size();
    		auto pos=lower_bound(st.begin(),st.end(),a[i].num+1);

    		if(*pos>=m){

    			ans+=*pos-a[i].num;
    			a[i].add+=*pos-a[i].num;
    			num[*pos-m]++;
    			num[a[i].num]--;
    			if(num[*pos-m]==n/m){
    				auto pos1=lower_bound(st.begin(),st.end(),*pos-m);
    				st.erase(pos1);
    				pos=lower_bound(st.begin(),st.end(),a[i].num+1);
    				st.erase(pos);
    			}
    		}
    		else{

    			ans+=*pos-a[i].num;
    			a[i].add+=*pos-a[i].num;
    			num[*pos]++;
    			num[a[i].num]--;
    			if(num[*pos]==n/m){
    				auto pos1=lower_bound(st.begin(),st.end(),*pos+m);
    				st.erase(pos1);
    				pos=lower_bound(st.begin(),st.end(),a[i].num+1);
    				st.erase(pos);
    			}
    		}
    	}

    }

    cout<<ans<<endl;
    for(int i=1;i<=n;i++) cout<<a[i].t+a[i].add<<' ';

}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;
set<ll>S;

int main(){
	FAST_IO;
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m);
	}
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	sort(all(vec));
	ll ans = 0;
	ll start;
	for(auto p : vec){
		start = *s.upper_bound(p.fst);
		start %= m;
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
		if(cnt[start]==up){
			S.erase(start);
			S.erase(start+m);
		}
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;
set<ll>S;

int main(){
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m+m);
	}
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	sort(all(vec));
	ll ans = 0;
	ll start;
	for(auto p : vec){
		start = *upper_bound(all(S),p.fst);
		start %= m;
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
		if(cnt[start]==up){
			S.erase(start);
			S.erase(start+m);
			S.erase(start+m*2);
		}
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;
set<ll>S;

int main(){
	FAST_IO;
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m);
	}
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	sort(all(vec));
	ll ans = 0;
	ll start;
	for(auto p : vec){
		start = *upper_bound(all(S),p.fst);
		start %= m;
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
		if(cnt[start]==up){
			S.erase(start);
			S.erase(start+m);
		}
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define CaseT int T;cin >> T;for(int cas=1;cas<=T;cas++)
#define clr(a,b) memset(a,b,sizeof(a))
#define sgn(a) ((a)==0?0:(a)/abs(a))
#define upd(a,b) (a%b?a/b+1:a/b)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fst first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int M = 1e6+20;
const int N = 2e5+20;

ll n,m;
ll a[N],t[N],cnt[N];
vector<pll>vec;
set<ll>S;

int main(){
	FAST_IO;
	cin >> n >> m;
	ll up = n/m;
	for(ll i=1;i<=n;i++){
		cin >> a[i];
		t[i] = a[i] % m;
		cnt[t[i]]++;
		if(cnt[t[i]]>up){
			vec.pb({t[i],i});
		}
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i);
	}
	for(int i=0;i<m;i++){
		if(cnt[i]<up) S.insert(i+m);
	}
	if(vec.size()==0){
		cout << 0 << endl;
		for(int i=1;i<=n;i++){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}
	sort(all(vec));
	ll ans = 0;
	ll start;
	for(auto p : vec){
		start = *S.upper_bound(p.fst);
		start %= m;
		a[p.sec] += (start-p.fst+m)%m;
		ans += (start-p.fst+m)%m;
		cnt[start]++;
		if(cnt[start]==up){
			S.erase(start);
			S.erase(start+m);
		}
	}
	cout << ans << endl;
	for(int i=1;i<=n;i++){
		cout << a[i] << ' ';
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
struct type
{
	int n;
	int id;
}A[200500];
int B[200500];
int P[200500]; //Point to the next free space
bool cmp(type A,type B)
{
	if(A.n < B.n) return true;
	else return A.id <= B.id;
}
bool cmp2(type A,type B)
{
	return A.id < B.id;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%d",&A[i].n);
		A[i].id = i;
	}

	sort(A,A+n,cmp);
	for (int i = 0;i < n;i++)
	{
		int r = A[i].n % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i].n += add;
		sum += add;
	}
	printf("%I64d\n",sum);
	sort(A,A+n,cmp2);
	for (int i = 0;i < n-1;i++)
	{
		printf("%d ",A[i]);
	}
	printf("%d\n",A[n-1]);
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
struct type
{
	long long int n;
	int id;
}A[200500];
int B[200500];
int P[200500]; //Point to the next free space
bool cmp(type A,type B)
{
	if(A.n < B.n) return true;
	else return A.id <= B.id;
}
bool cmp2(type A,type B)
{
	return A.id < B.id;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%I64d",&A[i].n);
		A[i].id = i;
	}

	sort(A,A+n,cmp);
	for (int i = 0;i < n;i++)
	{
		int r = A[i].n % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i].n += add;
		sum += add;
	}
	printf("%I64d\n",sum);
	sort(A,A+n,cmp2);
	for (int i = 0;i < n-1;i++)
	{
		printf("%I64d ",A[i].n);
	}
	printf("%I64d\n",A[n-1].n);
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
struct type
{
	long long int n;
	int id;
}A[200500];
int B[200500];
int P[200500]; //Point to the next free space
bool cmp(type A,type B)
{
	if(A.n < B.n) return true;
	else return A.id <= B.id;
}
bool cmp2(type A,type B)
{
	return A.id < B.id;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%I64d",&A[i].n);
		A[i].id = i;
	}

	sort(A,A+n,cmp);
	for (int i = 0;i < n;i++)
	{
		int r = A[i].n % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i].n += add;
		sum += add;
	}
	printf("%I64d\n",sum);
	sort(A,A+n,cmp2);
	for (int i = 0;i < n-1;i++)
	{
		printf("%I64d ",A[i]);
	}
	printf("%I64d\n",A[n-1]);
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
long long int A[200500];
int B[200500];
int P[200500]; //Point to the next free space
int main()
{
	int n,m;

	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%d",&A[i]);
		int r = A[i] % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i] += add;
		sum += add;
	}
	printf("%I64d\n",sum);
	for (int i = 0;i < n-1;i++)
	{
		printf("%I64d ",A[i]);
	}
	printf("%I64d\n",A[n-1]);
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
long long int A[200500];
int B[200500];
int P[200500]; //Point to the next free space
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%d",&A[i]);
		int r = A[i] % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i] += add;
		sum += add;
	}
	printf("%I64d\n",sum);
	for (int i = 0;i < n-1;i++)
	{
		printf("%I64d ",A[i]);
	}
	printf("%I64d\n",A[n-1]);
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
struct type
{
	long long int n;
	int id;
}A[200005];
int B[200005];
int P[200005]; //Point to the next free space
bool cmp(type A,type B)
{
	if(A.n < B.n) return true;
	else return A.id < B.id;
}
bool cmp2(type A,type B)
{
	return A.id < B.id;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%I64d",&A[i].n);
		A[i].id = i;
	}

	sort(A,A+n,cmp);
	for (int i = 0;i < n;i++)
	{
		int r = A[i].n % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i].n += add;
		sum += add;
	}
	printf("%I64d\n",sum);
	sort(A,A+n,cmp2);
	for (int i = 0;i < n-1;i++)
	{
		printf("%I64d ",A[i].n);
	}
	printf("%I64d\n",A[n-1].n);
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
struct type
{
	long long int n;
	int id;
}A[200005];
int B[200005];
int C[200005];
int P[200005]; //Point to the next free space
bool cmp(type A,type B)
{
	if(A.n < B.n) return true;
	else return A.id <= B.id;
}
bool cmp2(type A,type B)
{
	return A.id < B.id;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	memset(B,0,sizeof(B));
	long long int sum = 0;
	for (int i = 0;i < n;i++)
	{
		P[i] = i;
	}
	for (int i = 0;i < n;i++)
	{
		scanf("%I64d",&A[i].n);
		A[i].id = i;
	}

	sort(A,A+n,cmp);
	for (int i = 0;i < n;i++)
	{
		C[A[i].id] = i;
	}
	for (int i = 0;i < n;i++)
	{
		int r = A[i].n % m;
		//cout << r << " " << B[P[r]] << endl;
		if (B[P[r]] < n/m)
		{
			B[P[r]] ++;
		}
		else
		{
			while (B[P[r]] == n/m)
			{
				P[r] ++;
				if (P[r] >= m) P[r] = 0;
			}
			B[P[r]] ++;
		}
		int add = P[r] >= r?P[r] - r:m + P[r] - r;
		A[i].n += add;
		sum += add;
	}
	printf("%I64d\n",sum);

	for (int i = 0;i < n-1;i++)
	{
		printf("%I64d ",A[C[i]].n);
	}
	printf("%I64d\n",A[C[n-1]].n);
	//system("pause");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll n, m, a[MAXN], c[MAXN], ans;
set<int> s;
int main()
{
    scanf("%I64d%I64d", &n, &m);
    for (int i = 0; i < m; i++) s.insert(i);
    for (int i = 0; i < n; i++)
    {
        scanf("%I64d", &a[i]);
        int d = a[i]%m, x;
        //找到d能加到的最近的余数x
        if (d > *s.rbegin()) x = *s.begin();//d比最大的还大
        else x = *s.lower_bound(d);
        if (++c[x] == n/m) s.erase(x);//余数为x的数凑够了
        ans += (x-d+m)%m;
        a[i] += (x-d+m)%m;
    }
    printf("%I64d\n", ans);
    for (int i = 0; i < n; i++) printf("%I64d ", a[i]);
}


///be nam khoda///
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
long long int a[maxn];
int n;
int m;
long long int ans;
vector<int>v[maxn];
deque<int>deq;
inline void input();
inline void gpda();
inline void solved();
inline void tamam();
int main(){
  input();
  gpda();
  solved();
  tamam();
	return 0;
}
inline void input(){
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
	scanf("%d",&a[i]);
		v[a[i]%m].push_back(i);
	}
	return ;
}
inline void gpda(){
	for(int i=m-1;i>=0;i--){
		if(v[i].size()<(n/m)){
			deq.push_back(i);
		}
		else if(v[i].size()>(n/m)){
			while(v[i].size()!=(n/m)){
				if(deq.size()>0){
				v[deq.front()].push_back(v[i][v[i].size()-1]);
					if(v[deq.front()].size()==(n/m)){
						deq.pop_front();
					}
				v[i].pop_back();
			}
			else{
				v[0].push_back(v[i][v[i].size()-1]);
				v[i].pop_back();
			}
		}
		}
	}
	return ;
}
inline void solved(){
	for(int i=m-1;i>=0;i--){
		for(int j=0;j<v[i].size();j++){
			if((a[v[i][j]]%m)!=i){
				if((a[v[i][j]]%m)<i){
					ans+=i-(a[v[i][j]]%m);
					a[v[i][j]]+=i-(a[v[i][j]]%m);
				}
				else{
					ans+=i+(m-(a[v[i][j]]%m));
					a[v[i][j]]+=i+(m-(a[v[i][j]]%m));;
				}
			}
		}
	}
	return ;
}
inline void tamam(){
	printf("%I64d\n",ans);
	for(int i=0;i<n;i++){
		printf("%d ",a[i]);
	}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int m;
cin>>m;
int p[m];
for(int i=0;i<m;i++){
	p[i]=0;
}
int j[n];
for(int i=0;i<n;i++){
cin>>j[i];
int u=j[i]%m;
p[u]++;
}
int fes=0;
int pes=n/m;
for(int i=0;i<n;i++){
           int r=j[i];
           if(p[(j[i]%m)]>pes){
                      while(p[j[i]%m]>=pes){
                                 j[i]++;
                                 fes++;
                      }
                      p[j[i]%m]++;
                      p[r%m]--;
           }
}
cout<<fes<<endl;
for(int i=0;i<n;i++){
           cout<<j[i]<<" ";
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int m;
cin>>m;
int p[m];
for(int i=0;i<m;i++){
	p[i]=0;
}
int j[n];
for(int i=0;i<n;i++){
cin>>j[i];
int u=j[i]%m;
p[u]++;
}
long long int fes=0;
int pes=n/m;
for(int i=0;i<n;i++){
           int r=j[i];
           if(p[(j[i]%m)]>pes){
                      while(p[j[i]%m]>=pes){
                                 j[i]++;
                                 fes++;
                      }
                      p[j[i]%m]++;
                      p[r%m]--;
           }
}
cout<<fes<<endl;
for(int i=0;i<n;i++){
           cout<<j[i]<<" ";
}
return 0;
}


///be nam khoda///
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int b[maxn];
int n;
int m;
long long int ans;
vector<int>v[maxn];
deque<int>deq;
inline void input();
inline void gpda();
inline void solved();
inline void tamam();
int main(){
  input();
  gpda();
  solved();
  tamam();
	return 0;
}
inline void input(){
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
	scanf("%d",&a[i]);
		v[a[i]%m].push_back(i);
	}
	return ;
}
inline void gpda(){
	deq.push_back(0);
	for(int i=m-1;i>=0;i--){
		if(v[i].size()<(n/m)){
			deq.push_back(i);
		}
		else if(v[i].size()>(n/m)){
			while(v[i].size()!=(n/m)){
				v[deq.back()].push_back(v[i][v[i].size()-1]);
				if(deq.size()>1){
					if(v[deq.back()].size()==(n/m)){
						deq.pop_back();
					}
				}
				v[i].pop_back();
			}
		}
	}
	return ;
}
inline void solved(){
	for(int i=m-1;i>=0;i--){
		for(int j=0;j<v[i].size();j++){
			if((a[v[i][j]]%m)!=i){
				if((a[v[i][j]]%m)<i){
					ans+=i-(a[v[i][j]]%m);
					a[v[i][j]]+=i-(a[v[i][j]]%m);
				}
				else{
					ans+=i+(m-(a[v[i][j]]%m));
					a[v[i][j]]+=i+(m-(a[v[i][j]]%m));;
				}
			}
		}
	}
	return ;
}
inline void tamam(){
	printf("%d\n",ans);
	for(int i=0;i<n;i++){
		printf("%d ",a[i]);
	}
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<ii> g;
	LL ans=0;
	for (int mi=0; mi<2*m; mi++) {
		int i=mi%m;
		int sz=cnt[i].size();
		if (sz>(n/m)) {
			for (int j=sz-1; j>=n/m; j--) {
				g.push_back(ii(i, cnt[i][j]));
				cnt[i].pop_back();
			}
		}
		else if (g.size() && sz<(n/m)) {
			for (int j=sz; j<n/m; j++) {
				int gi=g.back().first, ind=g.back().second;
				g.pop_back();
				ans+=(LL)(i-gi+m)%m;
				a[ind]+=(i-gi+m)%m;
				cnt[i].push_back(0);
			}
		}
	}
	printf("%lld\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<ii> g;
	int ans=0;
	for (int mi=0; mi<2*m; mi++) {
		int i=mi%m;
		int sz=cnt[i].size();
		if (sz>(n/m)) {
			for (int j=sz-1; j>=n/m; j--) {
				g.push_back(ii(i, cnt[i][j]));
				cnt[i].pop_back();
			}
		}
		else if (sz<(n/m)) {
			for (int j=sz; j<n/m; j++) {
				int gi=g.back().first, ind=g.back().second;
				g.pop_back();
				ans+=(i-gi+m)%m;
				a[ind]+=(i-gi+m)%m;
				cnt[i].push_back(0);
			}
		}
	}
	printf("%d\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<ii> g;
	vector<int> l;
	for (int i=0; i<m; i++) {
		int sz=cnt[i].size();
		if (sz>(n/m)) {
			for (int j=0; j<sz-(n/m); j++)
				g.push_back(ii(i, cnt[i][j]));
		}
		else if (sz<(n/m)) {
			for (int j=sz; j<n/m; j++)
				l.push_back(i);
		}
	}
	int ans=0;
	for (int i=0; i<(int)l.size(); i++) {
		int gi=g[i].first, ind=g[i].second;
		ans+=(l[i]-gi+m)%m;
		a[ind]+=(l[i]-gi+m)%m;
	}
	printf("%d\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<ii> g;
	LL ans=0;
	for (int mi=0; mi<2*m; mi++) {
		int i=mi%m;
		int sz=cnt[i].size();
		if (sz>(n/m)) {
			for (int j=sz-1; j>=n/m; j--) {
				g.push_back(ii(i, cnt[i][j]));
				cnt[i].pop_back();
			}
		}
		else if (sz<(n/m)) {
			for (int j=sz; j<n/m; j++) {
				int gi=g.back().first, ind=g.back().second;
				g.pop_back();
				ans+=(LL)(i-gi+m)%m;
				a[ind]+=(i-gi+m)%m;
				cnt[i].push_back(0);
			}
		}
	}
	printf("%lld\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<int> g, l;
	//if ((int)cnt[0].size()>(n/m))
	//	g.push_back(0);
	for (int i=0; i<m; i++) {
		if ((int)cnt[i].size()>(n/m))
			g.push_back(i);
		else if ((int)cnt[i].size()<(n/m))
			l.push_back(i);
	}
	//if ((int)cnt[0].size()<(n/m))
	//	l.push_back(0);
	int I=0, J=0;
	int ans=0;
	//cout<<g.size()<<' '<<l.size()<<'\n';
	for (int i=0; i<(int)l.size(); i++) {
		for (int j=0; j<(int)cnt[l[i]].size(); j++) {
			a[cnt[g[I]][J]]+=(l[i]-g[I]+m)%m;
			ans+=(l[i]-g[I]+m)%m;
			J++;
			//cout<<J<<'\n'<<flush;
			if (J==(int)cnt[g[I]].size())
				I++;
		}
	}
	printf("%d\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<ii> g;
	LL ans=0;
	for (int mi=0; mi<2*m; mi++) {
		int i=mi%m;
		int sz=cnt[i].size();
		if (sz>(n/m)) {
			for (int j=sz-1; j>=n/m; j--) {
				g.push_back(ii(i, cnt[i][j]));
				cnt[i].pop_back();
			}
		}
		else if (sz<(n/m)) {
			for (int j=sz; j<n/m; j++) {
				int gi=g.back().first, ind=g.back().second;
				g.pop_back();
				ans+=(LL)(i-gi+m)%m;
				a[ind]+=(i-gi+m)%m;
				cnt[i].push_back(0);
			}
		}
	}
	printf("%d\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>
#include <climits>
#include <cstdlib>

const double Pi=acos(-1.0);
typedef long long LL;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

const int maxN=200005;

typedef pair<int, int> ii;

int n, m;
int a[maxN];

vector<int> cnt[maxN];

int main (int argc, char ** argv) {
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++) {
		scanf("%d", &a[i]);
		cnt[a[i]%m].push_back(i);
	}
	vector<ii> g;
	LL ans=0;
	for (int mi=0; mi<2*m; mi++) {
		int i=mi%m;
		int sz=cnt[i].size();
		if (sz>(n/m)) {
			for (int j=sz-1; j>=n/m; j--)
				g.push_back(ii(i, cnt[i][j]));
			cnt[i].resize(n/m);
		}
		else if (g.size() && sz<(n/m)) {
			for (int j=sz; j<n/m; j++) {
				int gi=g.back().first, ind=g.back().second;
				g.pop_back();
				ans+=(LL)(i-gi+m)%m;
				a[ind]+=(i-gi+m)%m;
				cnt[i].push_back(0);
				if (!g.size())
					break;
			}
		}
	}
	printf("%lld\n", ans);
	for (int i=0; i<n; i++)
		printf("%d ", a[i]);
	return 0;
}


#include<cstdio>
#include<queue>
#include<utility>
using namespace std;
queue <pair <long long, long long> > q[200010];
queue <pair <long long, long long> > que;
long long a[200010];
int main()
{
	long long n, m;
	scanf("%lld%lld", &n, &m);
	for(int i = 0; i < n; ++i)
	{
		scanf("%lld", &a[i]);
		q[a[i] % m].push(make_pair(i, a[i]));
	}
	long long ans = 0;
	for(int i = 0; i < m; ++i)
		while(q[i].size() > n / m)
		{
			pair <long long, long long> p=q[i].front();
			que.push(make_pair(p.first, p.second + 1)), q[i].pop(), ++ans;
		}
	while(!que.empty())
	{
		pair <long long, long long> p;
		while(!que.empty())
			p=que.front(),q[p.second % m].push(make_pair(p.first, p.second)), que.pop();
		for(int i = 0; i < m; ++i)
			while(q[i].size() > n / m)
				p=q[i].front(), que.push(make_pair(p.first, p.second + 1)), q[i].pop(), ++ans;
	}
	printf("%lld\n",ans);
	for(int i = 0; i < m; ++i)
		while(!q[i].empty())
		{
			pair <long long, long long> p=q[i].front();
			q[i].pop();
			a[p.first]=p.second;
		}
	for(int i = 0; i < n; ++i)
		printf("%lld ", a[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int N=2*1e5+3;
int n,m,b[N];
ll ans;
struct node{int yuan,yu,xu;}a[N];
bool comp(const node &i,const node &j){ return i.yu<j.yu;}
bool comp1(const node &i,const node &j){ return i.xu<j.xu;//还原一下
}
void work(){
	int k=n/m;
	for(int i=1;i<=n;i++){ b[i]=(i-1)/k;}
	for(int i=1;i<=n;i++){ ans+=abs(a[i].yu-b[i]);}//推的
	for(int i=1;i<=n;i++){
		if(a[i].yu!=b[i])//两数不相同
		{
			if(a[i].yu>b[i]){ a[i].yuan+=b[i]+m-a[i].yu;}
			if(a[i].yu<b[i]){ a[i].yuan+=b[i]-a[i].yu;}
		}
	}
}
int main(){

	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){ scanf("%d",&a[i].yuan); a[i].yu=a[i].yuan%m;
	a[i].xu=i;}//标记上位置
	sort(a+1,a+n+1,comp);
	work();
	sort(a+1,a+n+1,comp1);
	printf("%lld\n",ans);
	for(int i=1;i<=n;i++){
		printf("%d ",a[i].yuan);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
struct stru{
    int elem, i;
    stru(int &_elem,int &_i) {
        elem=_elem,i=_i;
    }
};
const int N=200010;
int n,m,a[N];
vector<int> val[N];
vector<stru> free;
int main(){
    scanf("%d%d",&n,&m);
    int k=n/m;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        val[a[i]%m].push_back(i);
    }
    long long ans=0;
    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();
            free.push_back(stru(elem,i));
        }
        while(int(val[cur].size())<k&&!free.empty()){
            int elem=free.back().elem;
            int mmod=free.back().i;
            free.pop_back();
            val[cur].push_back(elem);
            a[elem]+=i-mmod;
            ans+=i-mmod;
        }
    }
    printf("%lld\n",ans);
    for(int i=0;i<n;i++)
    printf("%d ",a[i]);
    return 0;
}


/*
对于余数 x 来说，若余数为 x 的数不够 n/m 个，那么就让他不变，若达到 n/m 个，就让他变为距离他最近的
没凑够的余数，可用一数组 C 来维护余数为 i 的数的个数，定义一个 set<int> 用于维护没凑够的余数。因此
可以对每一个数字求他的余数 d，然后找到 set 中第一个大于等于 d 的数字 x，然后 C[x]++，等到 C[x]=n/m
 就将 x 从 set 中删除。
*/
#include<bits/stdc++.h>
using namespace std;
const int N = 1000000+5;
int n, m, a[N], b[N], t, ans;
set<int> s;
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i ++ ) b[a[i] % m] ++;
	for (int i = 0; i < m; i ++ )
		if(b[i] < m) s.insert(i);
	t = n / m;
	for (int i = 1; i <= n; i ++ )
		if (b[a[i] % m] > t) {
			int u = a[i] % m, x, y;
			if (u > *s.end()) x = *s.begin();
			else x = *s.lower_bound(u);
			b[u] --; b[x] ++;
			if (b[x] == t) s.erase(x);
			a[i] += (x - u + m) % m;
			ans += (x - u + m) % m;
		}
	printf("%d\n", ans);
	for (int i = 1; i <= n; i ++ )printf("%d ", a[i]);
	return 0;
}


#include <bits/stdc++.h>
#include<vector>
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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1000005
#define MOD 1e9+7
#define E 1e-6
using namespace std;
long long n,m;
int a[N],c[N];
int ans;
set<int> s;
int main()
{
    cin>>n>>m;

    for(int i=0;i<m;i++)
        s.insert(i);

    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        int d=a[i]%m;
        int x;

        if(d>*s.rbegin())//找到d能加到的最近的余数x
            x=*s.begin();//d比最大的还大
        else
            x=*s.lower_bound(d);

        if(++c[x]==n/m)//余数为x的数凑够了
            s.erase(x);

        ans+=(x-d+m)%m;
        a[i]+=(x-d+m)%m;
    }

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

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
    int n,m,k;
    long long s=0;
    cin>>n>>m;
    k=n/m;
    vector<int> a(n);
    vector<vector<int> > v(m);
    vector<pair<int,int> > free;
    for(int i=0;i<n;i++){
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    for(int i=0;i<m*2;i++){
        int cur=i%m;
        while(v[cur].size()>k){
            int p=v[cur].back();
            v[cur].pop_back();
            free.push_back(make_pair(p,i));
        }
        while(free.size() && v[cur].size()<k){
            int element=free.back().first;
            int mod=free.back().second;
            free.pop_back();
            v[cur].push_back(element);
            int dj=i-mod;
            a[element]+=dj;
            s+=dj;
        }
    }
    cout<<s<<endl;
    for(int i=0;i<n;i++)
        cout<<a[i]<<' ';
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j + M - i) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
		}

		while (rem && cr[i] <= stcnt) {
			i = (i + 1) % M;
		}

		while (rem && cr[j] >= stcnt) {
			j = (j + 1) % M;
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j + M - i) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
		}

		while (rem && cr[j] >= stcnt) {
			j = (j + 1) % M;
		}

		while (rem && cr[i] <= stcnt) {
			i = (i + 1) % M;
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j + M - i) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
			bool flag = false;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
				if (i == j)
					flag = true;
			}
			if (flag) {
				j = (j + 1) % M;
				while (rem && cr[j] >= stcnt) {
					j = (j + 1) % M;
				}
			}
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
			}

			j = (i + 1) % M;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}

	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j - i + M) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
			}

			j = (i + 1) % M;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
			i = (i + 1) % M;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
			}

			j = (i + 1) % M;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j - i) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
		}

		while (rem && cr[i] <= stcnt) {
			i = (i + 1) % M;
		}
		while (rem && cr[j] >= stcnt) {
			j = (j + 1) % M;
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j + M - i) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			num[*it] += incre;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
		}

		while (rem && cr[j] >= stcnt) {
			j = (j + 1) % M;
		}

		while (rem && cr[i] <= stcnt) {
			i = (i + 1) % M;
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (int i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, unordered_set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j - i + M) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
			bool flag = false;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
				if (i == j) {
					flag = true;
				}
			}

			if (flag) {
			j = (i + 1) % M;
				while (rem && cr[j] >= stcnt) {
					j = (j + 1) % M;
				}
			}
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
			i = (i + 1) % M;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
			}

			j = (i + 1) % M;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <vector>
#include <algorithm>
#include <unordered_set>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;
const LL INF = 1e18;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, vector<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].push_back(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	j = (i + 1) % M;
	while (rem && cr[j] >= stcnt) {
		j = (j + 1) % M;
	}

	LL ans = 0;
	while (rem) {

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j - i + M) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			LL tmp = num[*it] + incre;
			if (tmp > INF) {
				k--;
				continue;
			}
			num[*it] = tmp;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
			bool flag = false;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
				if (i == j) {
					flag = true;
				}
			}

			if (flag) {
			j = (i + 1) % M;
				while (rem && cr[j] >= stcnt) {
					j = (j + 1) % M;
				}
			}
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
			i = (i + 1) % M;
			while (rem && cr[i] <= stcnt) {
				i = (i + 1) % M;
			}

			j = (i + 1) % M;
			while (rem && cr[j] >= stcnt) {
				j = (j + 1) % M;
			}
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (LL i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

const int maxn = 200000 + 10;

int N, M, a[maxn], need;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;
	need = N / M;
	vector<vector<int> > vvi(M);

	for (int i = 0; i < N; i++) {
		cin >> a[i];
		vvi[a[i] % M].push_back(i);
	}

	stack<pair<int, int> > sp;
	long long  ans = 0;
	for (int i = 0; i < 2 * M; i++) {
		int cur = i % M;
		while (vvi[cur].size() > need) {
			int ind = vvi[cur].back();
			vvi[cur].pop_back();
			sp.push(make_pair(cur, ind));
		}

		while (vvi[cur].size() < need && !sp.empty()) {
			pair<int, int> pii = sp.top(); sp.pop();
			int incre = (cur - pii.first + M) % M;
			int ind = pii.second;
			ans += incre;
			vvi[cur].push_back(ind);
			a[ind] += incre;
		}
	}

	cout << ans << endl;
	for (int i = 0; i < N; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;

}


#include <iostream>
#include <map>
#include <set>
#include <memory.h>
#include <algorithm>

using namespace std;
const int maxn = 200000 + 20;
typedef long long LL;

LL N, M;
LL num[maxn], cr[maxn];
map<LL, set<LL>> mis;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;

	for (LL i = 0; i < N; i++) {
		cin >> num[i];
		LL remaider = num[i] % M;
		++cr[remaider];
		mis[remaider].insert(i);
	}

	LL rem = 0, stcnt = N / M;
	for (LL i = 0; i < M; i++) {
		if (cr[i] != stcnt)
			rem++;
	}

	LL i = 0, j = 0;
	while (rem && i < M) {
		if (cr[i] > stcnt)
			break;
		i++;
	}

	LL ans = 0;
	while (rem) {
		j = (i + 1) % M;
		while (rem && cr[j] >= stcnt) {
			j = (j + 1) % M;
		}

		LL yu = cr[i] - stcnt;
		LL quesh = stcnt - cr[j];
		LL incre = (j + M - i) % M;

		auto it = mis[i].begin();
		for (LL k = 0; k < min(yu, quesh); k++) {
			num[*it] += incre;
			ans += incre;
			mis[i].erase(it++);
		}

		if(yu > quesh) {
			cr[j] = stcnt;
			cr[i] -= quesh;
			rem--;
		}
		else if(yu < quesh){
			cr[i] = stcnt;
			cr[j] += yu;
			rem--;
		}
		else {
			cr[i] = stcnt;
			cr[j] = stcnt;
			rem -= 2;
		}

		while (rem && cr[i] <= stcnt) {
			i = (i + 1) % M;
		}
	}
	cout << ans << endl;
	cout << num[0];
	for (int i = 1; i < N; i++) {
		cout << " " << num[i];
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

const int maxn = 200000 + 10;

int N, M, a[maxn], need;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> N >> M;
	need = N / M;
	vector<vector<int> > vvi(M);

	for (int i = 0; i < N; i++) {
		cin >> a[i];
		vvi[a[i] % M].push_back(i);
	}

	stack<pair<int, int> > sp;
	int ans = 0;
	for (int i = 0; i < 2 * M; i++) {
		int cur = i % M;
		while (vvi[cur].size() > need) {
			int ind = vvi[cur].back();
			vvi[cur].pop_back();
			sp.push(make_pair(cur, ind));
		}

		while (vvi[cur].size() < need && !sp.empty()) {
			pair<int, int> pii = sp.top(); sp.pop();
			int incre = (cur - pii.first + M) % M;
			int ind = pii.second;
			ans += incre;
			vvi[cur].push_back(ind);
			a[ind] += incre;
		}
	}

	cout << ans << endl;
	for (int i = 0; i < N; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;

}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 1e6, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (long long i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (long long i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] = N / K - bucket[i];

	long long pos = N;
	for (long long i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (long long i = num[pos] % K + 1; i < num[pos] % K + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	long long dejar, cont = 0;
	long long ans = 0;
	for (long long i = 0; i < N; i++)
	{
		if (bucket[num[i % N] % K] <= N / K)
			continue;
		if (num[i % N] % K + 1 != K)
			dejar = upper_bound(sum + num[i % N] % K + 1, sum + K, cont) - sum;
		else
			dejar = K;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i % N] % K + 1, cont) - sum;
		bucket[dejar]++;
		bucket[num[i % N] % K]--;
		ans += (dejar + K - num[i % N] % K) % K;
		num[i % N] += (dejar + K - num[i % N] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (long long i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 5e2 + 1, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = pos + 1; i < pos + K; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i] % K, cont) - sum;
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i]) % K;
		num[i] += (dejar + K - num[i]) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const int LN = 2e5 + 10, LK = 10, INF = 1e9;
int N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < num[pos] % K + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i % N] % K] <= N / K)
			continue;
		if (num[i % N] % K + 1 != K)
			dejar = upper_bound(sum + num[i % N] % K + 1, sum + K, cont) - sum;
		else
			dejar = K;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i % N] % K + 1, cont) - sum;
		bucket[dejar]++;
		bucket[num[i % N] % K]--;
		ans += (dejar + K - num[i % N] % K) % K;
		num[i % N] += (dejar + K - num[i % N] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const int LN = 1e6, LK = 10, INF = 1e9;
int N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] = N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < num[pos] % K + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i % N] % K] <= N / K)
			continue;
		if (num[i % N] % K + 1 != K)
			dejar = upper_bound(sum + num[i % N] % K + 1, sum + K, cont) - sum;
		else
			dejar = K;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i % N] % K + 1, cont) - sum;
		bucket[dejar]++;
		bucket[num[i % N] % K]--;
		ans += (dejar + K - num[i % N] % K) % K;
		num[i % N] += (dejar + K - num[i % N] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 2e5 + 1, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < pos + K; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i] % K, cont) - sum;
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i] % K) % K;
		num[i] += (dejar + K - num[i] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll int
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const int LN = 2e5 + 10, LK = 10, INF = 1e9;
int N, K, bucket[LN], num[LN];
vector < int > index[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++, index[num[i] % K].push_back(i);

	int pos = 0;
	long long ans = 0;
	for (int i = 0; i < K; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		pos = max(pos, i);
		for (int j = N / K; j < bucket[i]; j++)
		{
			while (bucket[pos % K] >= N / K)
				pos++;
			bucket[pos % K]++;
			ans += (pos - i) % K;
			num[index[i][j]] += (pos - i) % K;
		}
		bucket[i] = K;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define ll int
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);

const int LN = 2e5 + 10, LK = 10, INF = 1e9;
int N, M, K, bucket[LN], num[LN];
vector < int > index[LN];

int main()
{
	optimizar_io

		cin >> N >> M;
	K = N / M;
	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % M]++, index[num[i] % M].push_back(i);

	int pos = 0;
	long long ans = 0;
	for (int i = 0; i < M; i++)
	{
		if (bucket[num[i] % M] <= K)
			continue;
		pos = max(pos, i);
		for (int j = K; j < bucket[i]; j++)
		{
			while (bucket[pos % M] >= K)
				pos++;
			bucket[pos % M]++;
			ans += (pos - i) % M;
			num[index[i][j]] += (pos - i) % M;
		}
		bucket[i] = K;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 2e5 + 1, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = pos + 1; i < pos + K; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i] % K, cont) - sum;
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i]) % K;
		num[i] += (dejar + K - num[i]) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);

const long long LN = 1e6 + 5, LK = 10, INF = 1e9;
long long N, M, K, bucket[LN], num[LN];
vector < long long > index[LN];

int main()
{
	optimizar_io

		cin >> N >> M;
	K = N / M;
	for (long long i = 0; i < N; i++)
		cin >> num[i], index[num[i] % M].push_back(i);

	for (int i = 0; i < M; i++)
		bucket[i] = index[i].size();

	long long pos = 0;
	long long ans = 0;
	for (long long i = 0; i < M; i++)
	{
		if (bucket[num[i] % M] <= K)
			continue;
		pos = max(pos, i);
		for (long long j = K; j < bucket[i]; j++)
		{
			while (bucket[pos % M] >= K)
				pos++;
			bucket[pos % M]++;
			ans += (pos - i) % M;
			num[index[i][j]] += (pos - i) % M;
		}
		bucket[i] = K;
	}

	cout << ans << "\n";

	for (long long i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 2e5 + 1, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < pos + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i] % K + 1, cont) - sum;
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i] % K) % K;
		num[i] += (dejar + K - num[i] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);

const long long LN = 1e6, LK = 10, INF = 1e9;
long long N, M, K, bucket[LN], num[LN];
vector < long long > index[LN];

int main()
{
	optimizar_io

		cin >> N >> M;
	K = N / M;
	for (long long i = 0; i < N; i++)
		cin >> num[i], index[num[i] % M].push_back(i);

	for (int i = 0; i < M; i++)
		bucket[i] = index[i].size();

	long long pos = 0;
	long long ans = 0;
	for (long long i = 0; i < M; i++)
	{
		if (bucket[i] <= K)
			continue;
		pos = max(pos, i);
		for (long long j = K; j < bucket[i]; j++)
		{
			while (bucket[pos % M] >= K)
				pos++;
			bucket[pos % M]++;
			ans += (pos - i) % M;
			num[index[i][j]] += (pos - i) % M;
		}
		bucket[i] = K;
	}

	cout << ans << "\n";

	for (long long i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const int LN = 2e5 + 10, LK = 10, INF = 1e9;
int N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < pos + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i % N] % K] <= N / K)
			continue;
		if (num[i % N] % K + 1 != K)
			dejar = upper_bound(sum + num[i % N] % K + 1, sum + K, cont) - sum;
		else
			dejar = K;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i % N] % K + 1, cont) - sum;
		bucket[dejar]++;
		bucket[num[i % N] % K]--;
		ans += (dejar + K - num[i % N] % K) % K;
		num[i % N] += (dejar + K - num[i % N] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 2e5 + 1, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < pos + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i] % K, cont) - sum;
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i] % K) % K;
		num[i] += (dejar + K - num[i] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const long long LN = 2e5 + 10, LK = 10, INF = 1e9;
long long N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < pos + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
		{
			dejar = upper_bound(sum, sum + num[i] % K + 1, cont) - sum;
		}
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i] % K) % K;
		num[i] += (dejar + K - num[i] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const int LN = 1e6, LK = 10, INF = 1e9;
int N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] = N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < num[pos] % K + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i % N] % K] <= N / K)
			continue;
		if (num[i % N] % K + 1 != K)
			dejar = upper_bound(sum + num[i % N] % K + 1, sum + K, cont) - sum;
		else
			dejar = K;
		if (dejar == K)
			dejar = upper_bound(sum, sum + num[i % N] % K + 1, cont) - sum;
		bucket[dejar]++;
		bucket[num[i % N] % K]--;
		ans += (dejar + K - num[i % N] % K) % K;
		num[i % N] += (dejar + K - num[i % N] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);
streambuf *cin_backup = cin.rdbuf(), *cout_backup = cout.rdbuf();

const int LN = 2e5 + 10, LK = 10, INF = 1e9;
int N, K, bucket[LN], num[LN], faltan[LN], sum[LN];

int main()
{
	optimizar_io

		cin >> N >> K;

	for (int i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % K]++;

	for (int i = 0; i < K; i++)
		if (bucket[i] < N / K)
			faltan[i] += N / K - bucket[i];

	int pos = N;
	for (int i = 0; i < N; i++)
	{
		if (bucket[num[i] % K] >= N / K)
		{
			pos = i;
			break;
		}
	}

	for (int i = num[pos] % K + 1; i < pos + K + 1; i++)
		sum[i % K] += sum[(i - 1 + K) % K] + faltan[i % K];

	int dejar, cont = 0;
	long long ans = 0;
	for (int i = pos; i < pos + N; i++)
	{
		if (bucket[num[i] % K] <= N / K)
			continue;
		dejar = upper_bound(sum + num[i] % K + 1, sum + K, cont) - sum;
		if (dejar == K)
		{
			dejar = upper_bound(sum, sum + num[i] % K + 1, cont) - sum;
		}
		bucket[dejar]++;
		bucket[num[i] % K]--;
		ans += (dejar + K - num[i] % K) % K;
		num[i] += (dejar + K - num[i] % K) % K;
		cont++;
	}

	cout << ans << "\n";

	for (int i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define np next_permutation
#define all(x) (x).begin(), (x).end()
#define is_different(x, y) memcmp(x, y, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define out_range_map(x,y) ((x) < 0 || (y) < 0 || (i) == N || (j) == M)
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(NULL);

const long long LN = 1e6, LK = 10, INF = 1e9;
long long N, M, K, bucket[LN], num[LN];
vector < long long > index[LN];

int main()
{
	optimizar_io

		cin >> N >> M;
	K = N / M;
	for (long long i = 0; i < N; i++)
		cin >> num[i], bucket[num[i] % M]++, index[num[i] % M].push_back(i);

	long long pos = 0;
	long long ans = 0;
	for (long long i = 0; i < M; i++)
	{
		if (bucket[num[i] % M] <= K)
			continue;
		pos = max(pos, i);
		for (long long j = K; j < bucket[i]; j++)
		{
			while (bucket[pos % M] >= K)
				pos++;
			bucket[pos % M]++;
			ans += (pos - i) % M;
			num[index[i][j]] += (pos - i) % M;
		}
		bucket[i] = K;
	}

	cout << ans << "\n";

	for (long long i = 0; i < N; i++)
		cout << num[i] << " ";

	return 0;
}


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

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<int> h1(n, 0);
    vector<int> h2(n, 0);
    vector<int> deg(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      h1[y] = (1ll * h1[y] * 31ll + x) % MOD;
      h2[y] = (1ll * h2[y] * 37ll + x) % MOD;
      deg[y]++;
    }
    map<pair<int,pair<int, int>>, long long> grouping;
    for (int i = 0; i < n; ++i) {
      if (!deg[i]) continue;
      //assert(h1[i] || h2[i]);
      //if (!h1[i] && !h2[i]) cout << " FOUND " << i << endl;
      auto key = make_pair(deg[i], make_pair( h1[i], h2[i]));
      grouping[key] += p[i];
    }

    long long ans = 0ll;
    for (const auto& it : grouping) {
      ans = gcd(ans, it.second);
    }
    cout << ans << "\n";
  }
  return 0;
}


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

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<bool> iso(n, true);
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<int> h1(n, 0);
    vector<int> h2(n, 0);
    vector<int> deg(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      iso[y] = false;
      h1[y] = (h1[y] * 31ll + x) % MOD;
      h2[y] = (h2[y] * 37ll + x) % MOD;
      deg[y]++;
    }
    vector<bool> there(n, true);
    map<pair<int,pair<int, int> >, int> grouping;
    for (int i = 0; i < n; ++i) {
      if (iso[i]) continue;
      assert(h1[i] || h2[i]);
      auto key = make_pair(deg[i], make_pair( h1[i], h2[i]));
      if (grouping.find(key) != grouping.end()) {
        int ex = grouping[key];
        there[i] = false;
        assert (there[ex]);
        p[ex] += p[i];
      } else {
        grouping[key] = i;
      }
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      if (there[i] && !iso[i]) {
        ans = gcd(ans, p[i]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}


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

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<ll> a(n, 0);
  vector<bool> iso(n, true);
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<vector<int>> gr(n);
    vector<int> h1(n, 0);
    vector<int> h2(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      a[x] += p[y];
      gr[x].push_back(y);
      iso[y] = false;
      h1[y] = (h1[y] * 31ll + x) % MOD;
      h2[y] = (h2[y] * 37ll + x) % MOD;
    }
    vector<bool> there(n, true);
    map<pair<int, int>, int> grouping;
    for (int i = 0; i < n; ++i) {
      if (iso[i]) continue;
      pair<int, int> key = {h1[i], h2[i]};
      if (grouping.find(key) != grouping.end()) {
        int ex = grouping[key];
        there[i] = false;
        p[ex] += p[i];
      } else {
        grouping[key] = i;
      }
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      if (there[i] && !iso[i]) {
        ans = gcd(ans, p[i]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}


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

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<bool> iso(n, true);
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<int> h1(n, 0);
    vector<int> h2(n, 0);
    vector<int> deg(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      iso[y] = false;
      h1[y] = (h1[y] * 31ll + x) % MOD;
      h2[y] = (h2[y] * 37ll + x) % MOD;
      deg[y]++;
    }
    vector<bool> there(n, true);
    map<pair<int,pair<int, int> >, int> grouping;
    for (int i = 0; i < n; ++i) {
      if (iso[i]) continue;
      //assert(h1[i] || h2[i]);
      //if (!h1[i] && !h2[i]) cout << " FOUND " << i << endl;
      auto key = make_pair(deg[i], make_pair( h1[i], h2[i]));
      if (grouping.find(key) != grouping.end()) {
        int ex = grouping[key];
        there[i] = false;
        assert (there[ex]);
        p[ex] += p[i];
      } else {
        grouping[key] = i;
      }
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      if (there[i] && !iso[i]) {
        ans = gcd(ans, p[i]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}


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

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  while (q-- >0) {
    int n;
    cin >> n;
    int m;
    cin >> m;
    ll p[n];
    for (int i = 0 ;i < n; ++i) cin >> p[i];
    vector<ll> h1(n, 0);
    vector<ll> h2(n, 0);
    vector<vector<int>> gr(n);
    vector<int> deg(n, 0);
    for (int i  =0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      h1[y] = h1[y] * 31ll + x;
      h2[y] = h2[y] * 1000000007ll + x;
      gr[y].push_back(x);
      deg[y]++;
    }
    map<vector<ll>, long long> grouping;
    for (int i = 0; i < n; ++i) {
      if (!deg[i]) continue;
      //sort(gr[i].begin(), gr[i].end());
      auto key = {(ll) deg[i], h1[i], h2[i]};
      //auto key = gr[i];
      grouping[key] += p[i];
    }

    long long ans = 0ll;
    for (const auto& it : grouping) {
      ans = gcd(ans, it.second);
    }
    cout << ans << "\n";
  }
  return 0;
}