#ifndef ONLINE_JUDGE
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FORd(i, a, b) for(int i = a - 1; i >= b; i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pi> vii;
typedef vector<pl> vll;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int maxn = 6010;
int n;
int r[maxn];
vi adj[maxn];
int p[maxn];
int d[maxn];
int ans;

void dfs(int u) {
	FOR(i, 0, sz(adj[u])) {
		int v = adj[u][i];
		if (p[u] != v) {
			int k = lower_bound(d, d + n, r[v]) - d;
			ans = max(ans, k);
			int tmp = d[k];
			d[k] = r[v];
			p[v] = u; dfs(v);
			d[k] = tmp;
		}
	}
}

void solve() {
	scanf("%d", &n);
	FOR(i, 0, n) scanf("%d", r + i);
	FOR(i, 0, n - 1) {int a, b; scanf("%d%d", &a, &b); a--; b--; adj[a].push_back(b); adj[b].push_back(a);}
	ans = 0; ms(d, 0x3f);
	FOR(i, 0, n) {p[i] = -1; d[0] = r[i]; dfs(i);}
	printf("%d", ans + 1);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
	solve();
#ifndef ONLINE_JUDGE
	printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#ifndef ONLINE_JUDGE
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FORd(i, a, b) for(int i = a - 1; i >= b; i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pi> vii;
typedef vector<pl> vll;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int maxn = 6010;
int n;
int r[maxn];
vi adj[maxn];
int d[maxn];
int ans;

void dfs(int u) {
	ans = max(ans, d[u]);
	FOR(i, 0, sz(adj[u])) {
		int v = adj[u][i];
		d[v] = d[u] + 1;
		dfs(v);
	}
}

void solve() {
	scanf("%d", &n);
	FOR(i, 0, n) scanf("%d", r + i);
	FOR(i, 0, n - 1) {int a, b; scanf("%d%d", &a, &b); a--; b--; if (r[a] > r[b]) adj[b].push_back(a); if (r[b] > r[a]) adj[a].push_back(b);}
	ans = 0;
	FOR(i, 0, n) {
		ms(d, 0); d[i] = 1;
		dfs(i);
	}
	printf("%d", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
	solve();
#ifndef ONLINE_JUDGE
	printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#ifndef ONLINE_JUDGE
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FORd(i, a, b) for(int i = a - 1; i >= b; i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pi> vii;
typedef vector<pl> vll;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int maxn = 6010;
int n;
int r[maxn];
vi adj[maxn];
int cnt[maxn];
int p[maxn];
int ans;

void dfs(int u, set<int> st) {
	//bool flag = 1;
	set<int>::iterator it = st.lower_bound(-r[u]);
	if (it != st.end()) st.erase(*it);
	st.insert(-r[u]);
	ans = max(ans, sz(st));
	FOR(i, 0, sz(adj[u])) {
		int v = adj[u][i];
		if (p[u] != v) {
			//flag = 0;
			p[v] = u; dfs(v, st);
		}
	}
	//if (flag) {
		//set<int> st;
		//while (u != -1) {
			//set<int>::iterator it = st.lower_bound(-r[u]);
			//if (it != st.end()) st.erase(*it);
			//st.insert(-r[u]);
			//u = p[u];
		//}
		//ans = max(ans, sz(st));
	//}
}

void solve() {
	ms(cnt, 0);
	scanf("%d", &n);
	FOR(i, 0, n) scanf("%d", r + i);
	FOR(i, 0, n - 1) {int a, b; scanf("%d%d", &a, &b); a--; b--; adj[a].push_back(b); adj[b].push_back(a); cnt[a]++; cnt[b]++;}
	ans = 0;
	FOR(i, 0, n) {if (cnt[i] > 1) continue; p[i] = -1; set<int> st; dfs(i, st);}
	printf("%d", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
	solve();
#ifndef ONLINE_JUDGE
	printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#ifndef ONLINE_JUDGE
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FORd(i, a, b) for(int i = a - 1; i >= b; i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pi> vii;
typedef vector<pl> vll;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int maxn = 6010;
int n;
int r[maxn];
vi adj[maxn];
bool vis[maxn];
int cnt[maxn];
int p[maxn];
int ans;

void dfs(int u) {
	bool flag = 1;
	FOR(i, 0, sz(adj[u])) {
		int v = adj[u][i];
		if (!vis[v]) {
			flag = 0;
			vis[v] = 1;
			p[v] = u; dfs(v);
			vis[v] = 0;
		}
	}
	if (flag) {
		set<int> st;
		while (u != -1) {
			set<int>::iterator it = st.lower_bound(-r[u]);
			if (it != st.end()) st.erase(*it);
			st.insert(-r[u]);
			u = p[u];
		}
		ans = max(ans, sz(st));
	}
}

void solve() {
	ms(cnt, 0);
	scanf("%d", &n);
	FOR(i, 0, n) scanf("%d", r + i);
	FOR(i, 0, n - 1) {int a, b; scanf("%d%d", &a, &b); a--; b--; adj[a].push_back(b); adj[b].push_back(a); cnt[a]++; cnt[b]++;}
	ans = 0;
	FOR(i, 0, n) {if (cnt[i] > 1) continue; ms(vis, 0); vis[i] = 1; p[i] = -1; dfs(i);}
	printf("%d", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
	solve();
#ifndef ONLINE_JUDGE
	printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        if(v[i].size() <= r)
            continue;
        ll add;
        while(v[i].size() > r)
        {
            ll j=i+1;
            while(v[j].size() >= r)
                j = (j+1)%m;
            add = j-i;
            if(i > j)
                add += m;
            ans += add;
            a[v[i][v[i].size()-1]] += add;
            v[j].push_back(v[i][v[i].size()-1]);
            v[i].pop_back();
        }
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        if(v[i].size() <= r)
            continue;
        ll add;
        ll j=(i+1)%m;
        while(v[i].size() > r)
        {
            while(v[j].size() >= r)
                j = (j+1)%m;
            add = j-i;
            if(i > j)
                add += m;
            ans += add;
            a[v[i][v[i].size()-1]] += add;
            v[j].push_back(v[i][v[i].size()-1]);
            v[i].pop_back();
        }
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    vector<pair<ll,ll> > free;
    for(ll k=0;k<2*m;k++)
    {
        ll i=k%m;
        while(v[i].size() > r)
        {
            ll ele = v[i][v[i].size()-1];
            v[i].pop_back();
            free.push_back({ele,i});
        }
        while(free.size() > 0 && v[i].size() < r)
        {
            ll ele = free[free.size()-1].first;
            ll j = free[free.size()-1].second;
            free.pop_back();
            v[i].push_back(ele);
            a[ele] += i-j;
            ans += i-j;
        }
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    ll j=0;
    for(ll i=0;i<m;i++)
    {
        if(v[i].size() <= r)
            continue;
        ll add;
        while(v[i].size() > r)
        {
            while(v[j].size() >= r)
                j = (j+1)%m;
            add = j-i;
            if(i > j)
                add += m;
            ans += add%m;
            a[v[i][v[i].size()-1]] += add%m;
            v[j].push_back(v[i][v[i].size()-1]);
            v[i].pop_back();
        }
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    ll j=0;
    for(ll i=0;i<m;i++)
    {
        if(v[i].size() <= r)
            continue;
        ll add;
        while(v[i].size() > r)
        {
            while(v[j].size() >= r)
                j = (j+1)%m;
            if(i > j)
                add = m-i+j;
            else
                add = j-i;
            ans += add;
            a[v[i][v[i].size()-1]] += add;
            v[j].push_back(v[i][v[i].size()-1]);
            v[i].pop_back();
        }
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        while(v[i].size() > r)
        {
            v[(i+1)%m].push_back(v[i][v[i].size()-1]);
            a[v[i][v[i].size()-1]]++;
            ans++;
            v[i].pop_back();
        }
        v[i].shrink_to_fit();
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        if(v[i].size() <= r)
            continue;
        ll add;
        ll j=i+1;
        while(v[i].size() > r)
        {
            while(v[j].size() >= r)
                j = (j+1)%m;
            add = j-i;
            if(i > j)
                add += m;
            ans += add;
            a[v[i][v[i].size()-1]] += add;
            v[j].push_back(v[i][v[i].size()-1]);
            v[i].pop_back();
        }
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        while(v[i].size() > r)
        {
            v[(i+1)%m].push_back(v[i][v[i].size()-1]);
            a[v[i][v[i].size()-1]]++;
            ans++;
            v[i].pop_back();
        }
        v[i].clear();
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        while(v[i].size() > r)
        {
            v[(i+1)%m].push_back(v[i][v[i].size()-1]);
            a[v[i][v[i].size()-1]]++;
            ans++;
            v[i].pop_back();
        }
        vector<ll> v2;
        v[i] = v2;
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


//!
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll a[n];
    vector<ll> v[m];
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        v[a[i]%m].push_back(i);
    }
    ll r = n/m;
    ll ans = 0;
    for(ll i=0;i<m;i++)
    {
        while(v[i].size() > r)
        {
            v[(i+1)%m].push_back(v[i][v[i].size()-1]);
            a[v[i][v[i].size()-1]]++;
            ans++;
            v[i].pop_back();
        }
        v[i].shrink_to_fit();
    }
    cout<<ans<<"\n";
    for(ll i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<"\n";
    return 0;
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
vector<pair<int,int>> v;
int frq[200000];
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        int x ;
        cin >> x ;
        frq[x%m]++;
        v.pb({x,i});
    }
    sort(all(v));
    int ans =0 ;
    for (int i =0 ; i < n ;i++)
    {
        if ( frq[ v[i].f%m ] == n / m )continue ;
        while (frq[ v[i].f%m ] > n / m )
        {
            frq[ v[i].f%m ]--;
            v[i].f++;
            ans++;
            frq[ v[i].f%m ]++;
        }
    }
    for (int i =0 ; i < m ;i++)
    {
        if (frq[ i ] != n/m)
        {
            cout <<"ERER\n";
        }
    }
    cout << ans <<"\n";
    for (int i =0 ; i < n ;i++)
    {
        frq[ v[i].s ] = v[i].f ;
    }
    for (int i =0 ; i < n ;i++)
    {
        cout << frq[i] <<" ";
    }

}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            pq.pop();
            pq.push( {-frq[ a[i]%m ] , a[i]%m } );
        }
        if (pq.empty())break;
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }
    while(!pq.empty())pq.pop();

    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            pq.pop();
            pq.push( {-frq[ a[i]%m ] , a[i]%m } );
        }
        if (pq.empty())break;
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }

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


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
ll ans ;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            int idx = pq.top().s ;
            pq.pop();
            pq.push({-frq[idx],idx});
        }
        while (!pq.empty() && -pq.top().f > n/m)pq.pop();

        if (pq.empty())continue;
        auto cur = pq.top() ;
        pq.pop();
        ll rm = 0;
        if (a[i]%m > cur.s)
        {
            rm = a[i]%m ;

        }else
        {
            rm = cur.s - a[i]%m;
        }
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }
    memset(frq, 0 , sizeof frq );
    for (int i =0 ; i < n ;i++)frq[ a[i] % m ]++;
    for (int i =0 ; i < m ;i++)
    {
        assert(frq[i] == n/m);
    }
    printf ("%lld\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            pq.pop();
            pq.push( {-frq[ a[i]%m ] , a[i]%m } );
        }
        if (pq.empty())break;
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
int a[200000];
int frq[200000];
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%d" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < n ;i++)
    {
        while ( frq[ a[i] % m ] > n/m )
        {
            frq[ a[i]%m ]--;
            a[i]++;
            ans++;
            frq[ a[i]%m ]++;
        }
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%d " , a[i]);

}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            int idx = pq.top().s ;
            pq.pop();
            pq.push({-frq[idx],idx});
        }
        if (pq.empty())continue;
        while (-pq.top().f > n/m)pq.pop();
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }

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


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
const int M = 2e5 ;
ll a[M];
vector<int> adj[M];
ll ans ;
bool check ()
{
    for (int i =0 ; i < m ;i++)
    {
        if (SZ(adj[i]) != n/m)return 0;
    }
    return 1 ;
}
int main()
{
    cin >> n >> m ;
    int goal = n/m;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        adj[ a[i] % m ].pb(i);
    }
    queue<int> q;
    while (!check()){
    for (int i =0 ; i < m ;i++)
    {
        /// won't be empty ...
        while ( SZ(adj[i]) > goal )
        {
            q.push( adj[i].back() );
            adj[i].pop_back();
        }
        while (SZ(adj[i]) < goal && !q.empty())
        {
            int idx = q.front() ;
            q.pop();
            int cost = 0;
            /// from a[idx]%m - > i
            if (a[idx]%m < i )
                {
                    cost = i - a[idx]%m;
                    a[idx]+=cost;
                }else
                {
                    cost = i;
                    a[idx]+=i;

                }
                ans += cost ;
                adj[i].pb(idx);
        }
    }
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            int idx = pq.top().s ;
            pq.pop();
            pq.push({-frq[idx],idx});
        }
        if (pq.empty())continue;
        while (-pq.top().f > n/m)pq.pop();
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }
    memset(frq, 0 , sizeof frq );
    for (int i =0 ; i < n ;i++)frq[ a[i] % m ]++;
    for (int i =0 ; i < m ;i++)
    {
        assert(frq[i] == n/m);
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
const int M = 2e5 ;
ll a[M];
vector<int> adj[M];
ll ans ;
int main()
{
    cin >> n >> m ;
    int goal = n/m;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        adj[ a[i] % m ].pb(i);
    }
    queue<int> q;
    for (int i =0 ; i < m ;i++)
    {
        /// won't be empty ...
        while ( SZ(adj[i]) > goal )
        {
            q.push( adj[i].back() );
            adj[i].pop_back();
        }
        while (SZ(adj[i]) < goal && !q.empty())
        {
            int idx = q.front() ;
            q.pop();
            int cost = 0;
            /// from a[idx]%m - > i
            if (a[idx]%m < i )
                {
                    cost = i - a[idx]%m;
                    a[idx]+=cost;
                }else
                {
                    cost = i;
                    a[idx]+=i;

                }
                ans += cost ;
                adj[i].pb(idx);
        }
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            pq.pop();
            pq.push( {-frq[ a[i]%m ] , a[i]%m } );
        }
        if (pq.empty())break;
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }

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


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
const int M = 2e5 ;
ll a[M];
vector<int> adj[M];
ll ans ;
bool check ()
{
    for (int i =0 ; i < m ;i++)
    {
        if (SZ(adj[i]) != n/m)return 0;
    }
    return 1 ;
}
int main()
{
    cin >> n >> m ;
    int goal = n/m;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        adj[ a[i] % m ].pb(i);
    }
    queue<int> q;
    while (!check()){
    for (int i =0 ; i < m ;i++)
    {
        /// won't be empty ...
        while ( SZ(adj[i]) > goal )
        {
            q.push( adj[i].back() );
            adj[i].pop_back();
        }
        while (SZ(adj[i]) < goal && !q.empty())
        {
            int idx = q.front() ;
            q.pop();
            ll cost = 0;
            /// from a[idx]%m - > i
            if (a[idx]%m < i )
                {
                    cost = i - a[idx]%m;
                    a[idx]+=cost;
                }else
                {
                    cost = m - a[idx]%m + i ;
                    a[idx]+=cost;

                }
                ans += cost ;
                adj[i].pb(idx);
        }
    }
    }
    printf ("%lld\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
int a[200000];
int frq[200000];
priority_queue<pair<int,int>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%d" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            pq.pop();
            pq.push( {-frq[ a[i]%m ] , a[i]%m } );
        }
        if (pq.empty())break;
        auto cur = pq.top() ;
        pq.pop();
        int rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }

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


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
const int M = 2e5 ;
ll a[M];
vector<int> adj[M];
ll ans ;
bool check ()
{
    for (int i =0 ; i < m ;i++)
    {
        if (SZ(adj[i]) != n/m)return 0;
    }
    return 1 ;
}
int main()
{
    cin >> n >> m ;
    int goal = n/m;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        adj[ a[i] % m ].pb(i);
    }
    queue<int> q;
    while (!check()){
    for (int i =0 ; i < m ;i++)
    {
        /// won't be empty ...
        while ( SZ(adj[i]) > goal )
        {
            q.push( adj[i].back() );
            adj[i].pop_back();
        }
        while (SZ(adj[i]) < goal && !q.empty())
        {
            int idx = q.front() ;
            q.pop();
            ll cost = 0;
            /// from a[idx]%m - > i
            if (a[idx]%m < i )
                {
                    cost = i - a[idx]%m;
                    a[idx]+=cost;
                }else
                {
                    cost = m - a[idx]%m + i ;
                    a[idx]+=i;

                }
                ans += cost ;
                adj[i].pb(idx);
        }
    }
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
ll ans ;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            int idx = pq.top().s ;
            pq.pop();
            pq.push({-frq[idx],idx});
        }
        if (pq.empty())continue;
        while (-pq.top().f > n/m)pq.pop();
        auto cur = pq.top() ;
        pq.pop();
        ll rm = 0;
        if (a[i]%m > cur.s)
        {
            rm = a[i]%m ;

        }else
        {
            rm = cur.s - a[i]%m;
        }
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }
    memset(frq, 0 , sizeof frq );
    for (int i =0 ; i < n ;i++)frq[ a[i] % m ]++;
    for (int i =0 ; i < m ;i++)
    {
        assert(frq[i] == n/m);
    }
    printf ("%lld\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            int idx = pq.top().s ;
            pq.pop();
            pq.push({-frq[idx],idx});
        }
        if (pq.empty())continue;
        while (-pq.top().f > n/m)pq.pop();
        auto cur = pq.top() ;
        pq.pop();
        ll rm = 0;
        if (a[i]%m > cur.s)
        {
            rm = a[i]%m ;

        }else
        {
            rm = cur.s - a[i]%m;
        }
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }
    memset(frq, 0 , sizeof frq );
    for (int i =0 ; i < n ;i++)frq[ a[i] % m ]++;
    for (int i =0 ; i < m ;i++)
    {
        assert(frq[i] == n/m);
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
ll ans ;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    if (frq[i] < n/m)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            int idx = pq.top().s ;
            pq.pop();
            pq.push({-frq[idx],idx});
        }
        auto cur = pq.top() ;
        pq.pop();
        ll rm = 0;
        if (a[i]%m > cur.s)
        {
            rm = m - a[i]%m + cur.s  ;

        }else
        {
            rm = cur.s - a[i]%m;
        }
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }

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


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m;
const int M = 2e5 ;
ll a[M];
vector<int> adj[M];
ll ans ;
bool check ()
{
    for (int i =0 ; i < m ;i++)
    {
        if (SZ(adj[i]) != n/m)return 0;
    }
    return 1 ;
}
int main()
{
    cin >> n >> m ;
    int goal = n/m;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        adj[ a[i] % m ].pb(i);
    }
    queue<int> q;
    while (!check()){
    for (int i =0 ; i < m ;i++)
    {
        /// won't be empty ...
        while ( SZ(adj[i]) > goal )
        {
            q.push( adj[i].back() );
            adj[i].pop_back();
        }
        while (SZ(adj[i]) < goal && !q.empty())
        {
            int idx = q.front() ;
            q.pop();
            ll cost = 0;
            /// from a[idx]%m - > i
            if (a[idx]%m < i )
                {
                    cost = i - a[idx]%m;
                    a[idx]+=cost;
                }else
                {
                    cost = m - a[idx]%m + i ;
                    a[idx]+=cost;

                }
                ans += cost ;
                adj[i].pb(idx);
        }
    }
    }
    printf ("%d\n" ,ans );
    for (int i =0 ; i < n ;i++)
        printf ("%lld " , a[i]);
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll ;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std ;
int n ,m,ans;
ll a[200000];
int frq[200000];
priority_queue<pair<ll,ll>> pq ;
int main()
{
    cin >> n >> m ;
    for (int i =0 ; i < n ;i++)
    {
        scanf ("%lld" ,a+i);
        frq[ a[i] % m ]++;
    }
    for (int i =0 ; i < m ;i++)
    pq.push({-frq[i],1LL*i});

    for (int i =0 ; i < n ;i++)
    {
        if (frq[ a[i] % m ] <= n/m)continue ;
        while (!pq.empty() && pq.top().f != -frq[ pq.top().s ])
        {
            pq.pop();
            pq.push( {-frq[ a[i]%m ] , a[i]%m } );
        }
        if (pq.empty())continue;
        auto cur = pq.top() ;
        pq.pop();
        ll rm = abs(a[i]%m - cur.s);
        frq[ a[i]%m ]--;
        ans += rm ;
        a[i]+=rm;
        frq[ a[i]%m ]++;
        pq.push({-frq[ a[i]%m] , a[i]%m });
    }

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(tmp > *s.rbegin()) x = *s.begin();
            else x = *s.lower_bound(tmp);
            rub[x] ++;
            if(rub[x] == n/m) s.erase(x);
            ans += (x - tmp + m)%m;
            a[i] = (x - tmp + m)%m;
		// if(x>*s.rbegin()) x=*s.begin();
		// else
		// 	x=*s.lower_bound(tmp);
		// rub[x]++;
		// if(rub[x]==n/m) s.erase(x);
		// ans+=(x-tmp+m)%m;
		// a[i]+=(x-tmp+m)%m;
	}
	printf("%lld\n",ans);
	for(register int i=1;i<=n;i++)
		printf("%lld ",a[i]);puts("");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(x>*s.rbegin()) x=*s.begin();
		else
			x=*s.lower_bound(tmp);
		rub[x]++;
		if(rub[x]==n/m) s.erase(x);
		ans+=(x-tmp+m)%m;
		a[i]+=(x-tmp+m)%m;
	}
	printf("%lld\n",ans);
	for(register int i=1;i<=n;i++)
		printf("%lld ",a[i]);puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int MAX_N =2e5+9;
int N,M,T,S;
LL vec[MAX_N];
LL res[MAX_N]; // 当前模的个数
set<int> s;
LL out[MAX_N];
int main()
{
    while(cin>>N>>M){
        memset(res,0,sizeof(res));
        memset(out,0,sizeof(out));
        s.clear();
        for(int i=0;i<M;i++){
            s.insert(i);
        }
        for(int i=0;i<N;i++){
            scanf("%lld",&vec[i]);
        }
        LL ans =0 ;
        for(int i=0;i<N;i++){
            LL t = vec[i]%M;
            LL x ;
            if(t > *s.rbegin()) x = *s.begin();
            else x = *s.lower_bound(t);
            res[x] ++;
            if(res[x] == N/M) s.erase(x);
            ans += (x - t + M)%M;
            out[i] = (x - t + M)%M;
        }
        cout<<ans<<endl;
        for(int i=0;i<N;i++){
            printf("%lld ",vec[i] + out[i]);
        }
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(x>*s.rbegin()) x=*s.begin();
		else x= *s.lower_bound(tmp);
		  rub[x] ++;
            if(rub[x] == n/m) s.erase(x);
		ans+=(x-tmp+m)%m;
		a[i]+=(x-tmp+m)%m;

		// if(t > *s.rbegin()) x = *s.begin();
  //           else x = *s.lower_bound(t);

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(tmp>*s.rbegin()) x=*s.begin();
		else x= *s.lower_bound(tmp);
		rub[x]++;
		if(rub[x] == n/m) s.erase(x);
		ans+=(x-tmp+m)%m;
		a[i]+=(x-tmp+m)%m;
    }
	printf("%lld\n",ans);
	for(register int i=1;i<=n;i++)
		printf("%lld ",a[i]);puts("");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline int read()
{
	register int x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m,ans;
int a[N],rub[N];
set <int> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		int tmp=a[i]%m,x;
		if(x>*s.rbegin()) x=*s.begin();
		else
			x=*s.lower_bound(tmp);
		rub[x]++;
		if(rub[x]==n/m) s.erase(x);
		ans+=(x-tmp+m)%m;
		a[i]+=(x-tmp+m)%m;
	}
	printf("%d\n",ans);
	for(register int i=1;i<=n;i++)
		printf("%d ",a[i]);puts("");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;

		if(tmp > *s.rbegin()) x = *s.begin();
            else x = *s.lower_bound(tmp);
		rub[x]++;
		if(rub[x] == n/m) s.erase(x);
		ans+=(x-tmp+m)%m;
		a[i]+=(x-tmp+m)%m;

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(x>*s.rbegin()) x=*s.begin();
		else x= *s.lower_bound(tmp);
		rub[x]++;
		if(rub[x] == n/m) s.erase(x);
		// ans+=(x-tmp+m)%m;
		// a[i]+=(x-tmp+m)%m;
			ans += (x - tmp + m)%m;
            a[i] += (x - tmp + m)%m;

		// if(t > *s.rbegin()) x = *s.begin();
  //           else x = *s.lower_bound(t);
  //           res[x] ++;
  //           if(res[x] == N/M) s.erase(x);

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline int read()
{
	register int x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(x>*s.rbegin()) x=*s.begin();
		else
			x=*s.lower_bound(tmp);
		rub[x]++;
		if(rub[x]==n/m) s.erase(x);
		ans+=(x-tmp+m)%m;
		a[i]+=(x-tmp+m)%m;
	}
	printf("%lld\n",ans);
	for(register int i=1;i<=n;i++)
		printf("%lld ",a[i]);puts("");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <cctype>
#include <set>
#define maX(a,b) (a>b?a:B)
#define miN(a,b) (a<b?a:b)
#define ll long long
#define N 200010
using namespace std;
inline ll read()
{
	register ll x=0;
	register bool f=0;
	register char ch=getchar();
	while(!isdigit(ch))
	{
		f^=!(ch^'-');
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=((x+(x<<2))<<1)+(ch^'0');
		ch=getchar();
	}
	return f?-x:x;
}
inline void print(int x)
{
	register int sc[30]={0},tot=0;
	do
	{
		sc[++tot]=x%10;
		x/=10;
	}while(x);
	while(tot) putchar('0'+sc[tot--]);
}
int n,m;
ll ans;
ll a[N],rub[N];
set <ll> s;
int main()
{
	n=read();
	m=read();
	for(register int i=0;i<m;i++)
		s.insert(i);
	for(register int i=1;i<=n;i++)
	{
		a[i]=read();
		ll tmp=a[i]%m,x;
		if(tmp > *s.rbegin()) x = *s.begin();
            else x = *s.lower_bound(tmp);
            rub[x] ++;
            if(rub[x] == n/m) s.erase(x);
            ans += (x - tmp + m)%m;
            a[i] += (x - tmp + m)%m;
		// if(x>*s.rbegin()) x=*s.begin();
		// else
		// 	x=*s.lower_bound(tmp);
		// rub[x]++;
		// if(rub[x]==n/m) s.erase(x);
		// ans+=(x-tmp+m)%m;
		// a[i]+=(x-tmp+m)%m;
	}
	printf("%lld\n",ans);
	for(register int i=1;i<=n;i++)
		printf("%lld ",a[i]);puts("");
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <int> vec[Max];
ll ar[Max];

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	int n, m, cur; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) {
			int baki = cur - vec[i].size();
			for( int j=i+1; j<m; j++ ) {
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						int x = vec[i].front();
						ar[x] += j-i;
						ans += j-i;
						vec[i].pop_front();
						baki--;
					}
				}
			}
			for( int j=0; j<i; j++ ) {
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						int x = vec[i].front();
						ar[x] += i-j;
						ans += i-j;
						vec[i].pop_front();
						baki--;
					}
				}
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first < b.first;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, s=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size() - cur, i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=0; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
//cout << baki << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ll ad = rem[j];
						if(rem[j]==0) break;
						ans += ad;
						ar[x] += ad;
						//cout << "okkkkkkkk " << ar[x] << endl;
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				//;if( vec[rem[j]].size() == cur ) s++;
			}
			for( int j=0; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
//cout << baki << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ll ad = rem[j];
						if(rem[j]==0) ad=m-i;
						ans += ad;
						ar[x] += ad;
						//cout << "okkkkkkkk " << ar[x] << endl;
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				//;if( vec[rem[j]].size() == cur ) s++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first < b.first;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	ll n, m, cur, s=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size() - cur, i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=s; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
//cout << baki << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ll ad = rem[j];
						if(rem[j]==0) ad = m-i;
						ans += ad;
						ar[x] += ad;
						//cout << "okkkkkkkk " << ar[x] << endl;
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				if( vec[rem[j]].size() == cur ) s++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	int n, m, cur; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) {
			ll baki = cur - vec[i].size();
			for( int j=i+1; j<m; j++ ) {
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ll y = i;
						if( y == 0 ) y = m;
						ll tot = (ar[x]/y)*y+y;
						ans += tot-ar[x];
						ar[x] = tot;
						//cout << ": " << i << " " << tot << endl;
						vec[j].pop_front();
						vec[i].push_back( tot );
						baki--;
					}
				}
			}
			for( int j=0; j<i; j++ ) {
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ll y = i;
						if( y == 0 ) y = m;
						ll tot = (ar[x]/y)*y+y;
						ans += tot-ar[x];
						ar[x] = tot;
						//cout << ": " << i << " " << tot << endl;
						vec[j].pop_front();
						vec[i].push_back( tot );
						baki--;
					}
				}
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first < b.first;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, s=0, f=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) {
			rem.push_back( i );
			if( i == 0 ) {
				s = 1;
				f = 1;
			}
		}
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size() - cur, i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=s; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
//cout << baki << " : " << vec[j].size() << endl;
				if( m-i <= j && f ) {
					while( baki && vec[0].size() < cur ) {
						ll x = vec[i].front();
						ll ad = m-i;
						ans += ad;
						ar[x] += ad;
						//cout << "okkkkkkkk " << ar[x] << endl;
						vec[i].pop_front();
						vec[0].push_back( ar[x] );
						baki--;
					}
				}
				while( baki && vec[rem[j]].size() < cur ) {
					ll x = vec[i].front();
					ll ad = rem[j];
					ans += ad;
					ar[x] += ad;
					//cout << "okkkkkkkk " << ar[x] << endl;
					vec[i].pop_front();
					vec[rem[j]].push_back( ar[x] );
					baki--;
				}
				if( vec[rem[j]].size() == cur ) s++;
			}
			while( f && baki && vec[0].size() < cur ) {
				ll x = vec[i].front();
				ll ad = m-i;
				ans += ad;
				ar[x] += ad;
				//cout << "okkkkkkkk " << ar[x] << endl;
				vec[i].pop_front();
				vec[0].push_back( ar[x] );
				baki--;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first < b.first;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, s=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size() - cur, i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=s; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
				//cout << j << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ans += rem[j];
						ar[x] += rem[j];
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				if( vec[rem[j]].size() == cur ) s++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first * a.second < b.first * b.second;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, s=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size() - cur, i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=s; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
				//cout << j << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ans += rem[j];
						ar[x] += rem[j];
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				if( vec[rem[j]].size() == cur ) s++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;
int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	ll n, m, cur, st=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=st; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
				//cout << j << " : " << vec[j].size() << endl;
				if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ans += rem[j];
						ar[x] += rem[j];
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				}
				if( vec[rem[j]].size() == cur ) st++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;
int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	int n, m, cur; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) rem.push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) {
			ll baki = cur - vec[i].size();
			for( int j=0; j<rem.size(); j++ ) {
				if(!baki)  break;
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ans += i;
						ar[x] += i;
						vec[j].pop_front();
						vec[i].push_back( ar[x] );
						baki--;
					}
				}
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

int tot[Max];
int ar[Max];

vector <int> vec[Max];

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif /// Mr_Emrul

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n, m, need;
	ll ans = 0;
	cin >> n >> m;
	need = n / m;
	for(int i = 1; i <= n; i++) {
		cin >> ar[i];
		vec[ar[i] % m].push_back(i);
		tot[ar[i] % m] += 1;
	}
	set <int> st;
	for(int i = 0; i < m; i++) {
		if(tot[i] < need) {
			st.insert(i);
			//printf("[%d]\n", i);
		}
	}
	for(int i = 0; i < m; i++) {
		if(tot[i] > need) {
			//printf(">> %d\n", i);
			auto it = st.lower_bound(i);
			vector <int> bad;
			while(it != st.end() && tot[i] > need) {
				int mn = min(tot[i] - need, need - tot[*it]);
				//printf("%d %d\n", *it, tot[*it]);
				while(mn--) {
					int x = vec[i].back();
					ar[x] += *it - i;
					ans += *it - i;
					vec[i].pop_back();
					tot[i]--;
					tot[*it]++;
					vec[*it].push_back(x);
					if(tot[*it] == need) {
						bad.push_back(*it);
					}
				}
				it++;
			}
			for(int it : st) {
				//printf("%d %d\n", it, tot[it]);
				int mn = min(tot[i] - need, need - tot[it]);
				if(tot[i] == need) {
					break;
				}
				while(mn--) {
					int x = vec[i].back();
					ar[x] += m - i + it;
					ans += m - i + it;
					vec[i].pop_back();
					tot[i]--;
					tot[it]++;
					vec[it].push_back(x);
				}
				if(tot[it] == need) {
					bad.push_back(it);
				}
			}
			for(int x : bad) {
				st.erase(x);
			}
		}
	}
	cout << ans << endl;
	for(int i = 1; i <= n; i++) {
		cout << ar[i] << " ";
	} cout << endl;
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;
int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	int n, m, cur; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) rem.push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) {
			ll baki = cur - vec[i].size();
			int id = 0;
			int l=0, r=rem.size()-1;
			while( l<=r ) {
				ll mid = (l+r)>>1;
				if( rem[mid] > i ) {
					id = mid;
					r = mid - 1;
				}
				else {
					l = mid + 1;
				}
			}
 			for( int j=id; j<rem.size(); j++ ) {
				if(!baki)  break;
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ll y = i;
						if( y == 0 ) y = m;
						ll tot = (ar[x]/y)*y+y;
						ans += tot-ar[x];
						ar[x] = tot;
						//cout << ": " << i << " " << tot << endl;
						vec[j].pop_front();
						vec[i].push_back( tot );
						baki--;
					}
				}
			}
			for( int j=0; j<rem.size(); j++ ) {
				if(!baki)  break;
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ll y = i;
						if( y == 0 ) y = m;
						ll tot = (ar[x]/y)*y+y;
						ans += tot-ar[x];
						ar[x] = tot;
						//cout << ": " << i << " " << tot << endl;
						vec[j].pop_front();
						vec[i].push_back( tot );
						baki--;
					}
				}
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;
int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, st=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=st; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
				//cout << j << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ans += rem[j];
						ar[x] += rem[j];
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				//if( vec[rem[j]].size() == cur ) st++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first < b.first;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, s=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size() - cur, i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=0; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
				//cout << j << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ans += rem[j];
						ar[x] += rem[j];
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				//;if( vec[rem[j]].size() == cur ) s++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];
vector <ll> rem;

bool cmp( pll a, pll b ) {
	return a.first > b.first;
}

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	ll n, m, cur, s=0; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) rem.push_back( i );
	}
	vector <pll> st;
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() > cur ) {
			st.push_back( {vec[i].size(), i} );
		}
	}
	sort( st.begin(), st.end(), cmp );
	for( pll it : st ) {
		ll i = it.second;
		if( vec[i].size() > cur ) {
			ll baki = vec[i].size() - cur;
			//cout << i << endl;
			for( int j=s; j<rem.size(); j++ ) {
				if(!baki)  break;
				//if( j == i ) continue
				//cout << j << " : " << vec[j].size() << endl;
				//if( vec[rem[j]].size() < cur ) {
					while( baki && vec[rem[j]].size() < cur ) {
						ll x = vec[i].front();
						ans += rem[j];
						ar[x] += rem[j];
						vec[i].pop_front();
						vec[rem[j]].push_back( ar[x] );
						baki--;
					}
				//}
				if( vec[rem[j]].size() == cur ) s++;
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
		assert( ar[i] <= 1e18 );
	}
	return 0;
}


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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

list <ll> vec[Max];
ll ar[Max];

int main() {
#ifdef Mr_Emrul
	freopen("inputf.in", "r", stdin);
#endif ///Mr_Emrul

	//ios_base::sync_with_stdio(false);
	//cin.tie(0);

	int n, m, cur; ll ans = 0;

	cin >> n >> m;

	cur = n / m;

	for( int i=1; i<=n; i++ ) {
		cin >> ar[i];
		vec[ar[i] % m].push_back( i );
	}
	for( int i=0; i<m; i++ ) {
		if( vec[i].size() < cur ) {
			ll baki = cur - vec[i].size();
			for( int j=i+1; j<m; j++ ) {
				if(!baki)  break;
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ll y = i;
						if( y == 0 ) y = m;
						ll tot = (ar[x]/y)*y+y;
						ans += tot-ar[x];
						ar[x] = tot;
						//cout << ": " << i << " " << tot << endl;
						vec[j].pop_front();
						vec[i].push_back( tot );
						baki--;
					}
				}
			}
			for( int j=0; j<i; j++ ) {
				if(!baki)  break;
				if( vec[j].size() > cur ) {
					while( baki && vec[j].size() > cur ) {
						ll x = vec[j].front();
						ll y = i;
						if( y == 0 ) y = m;
						ll tot = (ar[x]/y)*y+y;
						ans += tot-ar[x];
						ar[x] = tot;
						//cout << ": " << i << " " << tot << endl;
						vec[j].pop_front();
						vec[i].push_back( tot );
						baki--;
					}
				}
			}
		}
		//cout << i << " " << vec[i].size() << endl;
	}
	/*for( int i=0; i<m; i++ ) {
		cout << i << " " << vec[i].size() << endl;
	}*/

	cout << ans << endl;
	for( int i=1; i<=n; i++ ) {
		cout << ar[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
struct node
{
        int id , v;
        node(){};
        node( int x , int y )
        {
                id = x;
                v = y;
        }
        bool operator < ( const node &R ) const
        {
                if( v == R.v ) return id > R.id;
                return v > R.v;
        }
};
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();

        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
        }
        int req = n/m;
        long long ans = 0;
        int last = m-1;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req )
                {
                        int need = req - cnt[ i ];
                        int r = i == 0 ? m : i;
                        if( last >= r ) last--;
                        while( last >= 0 )
                        {
                                if( cnt[ last ] > req ){
                                        if( need <= cnt[ last ] )
                                        {
                                                ans += ( need*(r - last ) ) ;
                                                cnt[ last ] -= need;
                                                while( need-- )
                                                {
                                                        int idx= v[ last ].back();
                                                        ar[ idx ] += ( r - last );
                                                        v[ last ].pop_back();
                                                }
                                                break;
                                        }
                                        else{
                                                need -= cnt[ last ];
                                                for( int j = 0 ; j < cnt[ last ] ; j++ )
                                                {
                                                        int idx= v[ last ].back();
                                                        ar[ idx ] += ( r - last );
                                                        v[ last ].pop_back();
                                                }
                                                ans += ( cnt[ last ]*( r - last ) );
                                                cnt[ last ] = 0;
                                        }
                                }
                                last--;
                        }

                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        //freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
struct node
{
        int id , v;
        node(){};
        node( int x , int y )
        {
                id = x;
                v = y;
        }
        bool operator < ( const node &R ) const
        {
                if( v == R.v ) return id > R.id;
                return v > R.v;
        }
};
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();

        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
        }
        int req = n/m;
        long long ans = 0;
        priority_queue< node > pq;
        int last = m;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req )
                {
                        int need = req - cnt[ i ];
                        int r = i == 0 ? m-1 : i-1;
                        long long cc = 1;
                        last = min( r , last );
                        while( last >= 0 )
                        {
                                if( cnt[ last ] > req ){
                                        if( need <= cnt[ last ] )
                                        {
                                                ans += ( need*(i - last ));
                                                cnt[ last ] -= need;
                                                while( need-- )
                                                {
                                                        int idx= v[ last ].back();
                                                        ar[ idx ] += ( i - last );
                                                        v[ last ].pop_back();
                                                }
                                                break;
                                        }
                                        else{
                                                need -= cnt[ last ];
                                                for( int j = 0 ; j < cnt[ last ] ; j++ )
                                                {
                                                        int idx= v[ last ].back();
                                                        ar[ idx ] += (i - last );
                                                        v[ last ].pop_back();
                                                }
                                                ans += ( cnt[ last ]*(i - last ) );
                                                cnt[ last ] = 0;
                                        }
                                }
                                last--;
                        }

                }
        }
        printf("%lld\n",abs(ans));
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        //freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
struct node
{
        int id , v;
        node(){};
        node( int x , int y )
        {
                id = x;
                v = y;
        }
        bool operator < ( const node &R ) const
        {
                if( v == R.v ) return id > R.id;
                return v > R.v;
        }
};
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();
        set< int > st;
        set< int > :: iterator it , point;
        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
                st.insert( x );
        }
        int req = n/m;
        long long ans = 0;
        int last = m-1;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req )
                {
                        int need = req - cnt[ i ];
                        for( it = st.begin(); it != st.end() ; it++ )
                        {
                                int j = *it;
                                if( i == j ) continue;
                                int besi = cnt[ j ] - req;
                                int mn = min( besi , need );
                                if( besi > 0 )
                                {
                                        cnt[ j ] -= mn;
                                        int r = i < j ? (i + m) - j : i - j;
                                        while( need-- && mn-- && v[ j ].size() )
                                        {
                                                int vx = v[ j ].back();
                                                ar[ vx ] += r;
                                                v[ j ].pop_back();
                                                ans += r;
                                        }
                                        if( v[ j ].size() == 0 )
                                        {

                                                st.erase( j );
                                        }
                                }
                                if( need == 0 ) break;
                        }
                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        //freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
struct node
{
        int id , v;
        node(){};
        node( int x , int y )
        {
                id = x;
                v = y;
        }
        bool operator < ( const node &R ) const
        {
                if( v == R.v ) return id > R.id;
                return v > R.v;
        }
};
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();

        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
        }
        int req = n/m;
        long long ans = 0;
        priority_queue< node > pq;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req )
                {
                        int need = req - cnt[ i ];
                        int r = i == 0 ? m-1 : i-1;
                        long long cc = 1;
                        while( r >= 0 )
                        {
                                if( cnt[ r ] > req ){
                                        if( need <= cnt[ r ] )
                                        {
                                                ans += ( need*cc);
                                                cnt[ r ] -= need;
                                                while( need-- )
                                                {
                                                        int idx= v[ r ].back();
                                                        ar[ idx ] += cc;
                                                        v[ r ].pop_back();
                                                }
                                                break;
                                        }
                                        else{
                                                need -= cnt[ r ];
                                                for( int j = 0 ; j < cnt[ r ] ; j++ )
                                                {
                                                        int idx= v[ r ].back();
                                                        ar[ idx ] += cc;
                                                        v[ r ].pop_back();
                                                }
                                                ans += ( cnt[ r ]*cc );
                                                cnt[ r ] = 0;
                                        }
                                }
                                cc++;
                                r--;
                        }

                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        //freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
struct node
{
        int id , v;
        node(){};
        node( int x , int y )
        {
                id = x;
                v = y;
        }
        bool operator < ( const node &R ) const
        {
                if( v == R.v ) return id > R.id;
                return v > R.v;
        }
};
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();

        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
        }
        int req = n/m;
        long long ans = 0;
        int last = m-1;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req )
                {
                        int need = req - cnt[ i ];
                        for( int j = 0 ; j <= m ; j++ )
                        {
                                if( i == j ) continue;
                                int besi = cnt[ j ] - req;
                                int mn = min( besi , need );
                                if( besi > 0 )
                                {
                                        cnt[ j ] -= mn;
                                        int r = i < j ? (i + m) - j : i - j;
                                        while( need-- && mn--)
                                        {
                                                ar[ v[ j ].back() ] += r;
                                                v[ j ].pop_back();
                                                ans += r;
                                        }
                                }
                                if( need == 0 ) break;
                        }
                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        //freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
struct node
{
        int id , v;
        node(){};
        node( int x , int y )
        {
                id = x;
                v = y;
        }
        bool operator < ( const node &R ) const
        {
                if( v == R.v ) return id > R.id;
                return v > R.v;
        }
};
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();

        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
        }
        int req = n/m;
        long long ans = 0;
        int last = m-1;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req )
                {
                        int need = req - cnt[ i ];
                        int r = i == 0 ? m : i;
                        if( last >= r ) last--;
                        while( last >= 0 )
                        {
                                if( cnt[ last ] > req ){
                                        if( need <= cnt[ last ] )
                                        {
                                                ans += ( need*(r - last ) ) ;
                                                cnt[ last ] -= need;
                                                while( need-- )
                                                {
                                                        int idx= v[ last ].back();
                                                        ar[ idx ] += ( r - last );
                                                        v[ last ].pop_back();
                                                }
                                                break;
                                        }
                                        else{
                                                need -= cnt[ last ];
                                                for( int j = 0 ; j < cnt[ last ] ; j++ )
                                                {
                                                        int idx= v[ last ].back();
                                                        ar[ idx ] += ( r - last );
                                                        v[ last ].pop_back();
                                                }
                                                ans += ( cnt[ last ]*( r - last ) );
                                                cnt[ last ] = 0;
                                        }
                                }
                                last--;
                        }

                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


#include <bits/stdc++.h>
#define mem( a , b ) memset( a , b  , sizeof( a ) )
using namespace std;
int _I(){ int x; scanf("%d",&x); return x; }
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 };
bool ok( int i , int j , int x , int y )
{
        if( i < 0 || i >= x || j < 0 || j >= y ) return 0;
        return 1;
}
int cnt[ 200001 ];
long long ar[ 200001 ];
vector< int > v[ 200001 ];
void solve()
{
        int n = _I();
        int m = _I();

        for( int i = 0 ; i < n ; i++ )
        {
                int x = _I();
                ar[ i ]= (long long ) x;
                x %= m;
                cnt[ x ]++;
                v[ x ].push_back( i );
        }
        int req = n/m;
        long long ans = 0;
        for( int i = 0 ; i < m ; i++ )
        {
                if( cnt[ i ] < req && cnt[ i ] )
                {
                        int need = req - cnt[ i ];
                        int r = i == 0 ? n-1 : i-1;
                        long long cc = 1;
                        //cout<<i<<" "<<need<<" "<<
                        while( cnt[ r ] && r >= 0 )
                        {
                                cout<<r<<" "<<cnt[ r ]<<"\n";
                                if( cnt[ r ] > req ){
                                        int sz = v[ r ].size();
                                        if( need <= cnt[ r ] )
                                        {
                                                ans += ( need*cc);
                                                cnt[ r ] -= need;
                                                while( need-- )
                                                {
                                                        int idx= v[ r ][ --sz ];
                                                        ar[ idx ] += cc;
                                                        v[ r ].pop_back();
                                                }
                                                break;
                                        }
                                        else{
                                                need -= cnt[ r ];
                                                for( int j = 0 ; j < cnt[ r ] ; j++ )
                                                {
                                                        int idx= v[ r ][ --sz ];
                                                        ar[ idx ] += cc;
                                                        v[ r ].pop_back();
                                                }
                                                ans += ( cnt[ r ]*cc );
                                                cnt[ r ] = 0;
                                        }
                                }
                                cc++;
                                r--;
                        }

                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld " , ar[ i ] );
        }
}
int main()
{
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}