#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 1;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;

    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    int frrr = 0;
    int ops = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k){
            free[i]+=vec[i]-k;
            frrr += vec[i] - k;
        }
        else if(vec[i] < k and frrr > 0) {
            for(int j=i-1;j>=0;j--){
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                if(vec[i] >= k) break;
            }
            if(vec[i] >= k) break;
            for(int j=m-1;j>i;j--) {
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                if(vec[i] >= k) break;
            }
        }
    }
    //cout << ans << endl;

    stack<s> st;
    int tt = 2;
    ans = 0;
    int opss = 0;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                ans += kk;
                matrix[i].push_back({hell.loc,hell.val + kk});
                opss++;
            }
        }
    }
    cout << ops << " " << opss << endl;
    //cout << ans << endl;

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    //cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 0;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;

    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    int frrr = 0;
    int ops = 0;
    int opz = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k){
            free[i]+=vec[i]-k;
            frrr += vec[i] - k;
        }
        else if(vec[i] < k and frrr > 0) {
            for(int j=i-1;j>=0 and vec[i] < k and frrr > 0;j--){
                while(free[j] > 0 and vec[i]<k and frrr > 0){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                opz++;
            }
            for(int j=m-1;j>i and vec[i]< k and  frrr > 0;j--) {
                while(free[j] > 0 and vec[i]<k and frrr > 0){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                opz++;
            }
        }
    }

    stack<s> st;
    int tt = 2;
    ans = 0;
    int opss = 0;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                ans += kk;
                matrix[i].push_back({hell.loc,hell.val + kk});
                opss++;
            }
        }
    }
    //if(n == 200000) debug = 1;
    dout << ops<< " " << opz << " " << opss << endl;
    cout << ans << endl;

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 1;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;
    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k)
            free[i]+=vec[i]-k;
        else {
            for(int j=i-1;j>=0;j--)
                while(free[j] > 0 and vec[i]<k){
                    ans += (i-j+m)%m;
                    vec[i]++;
                    free[j]--;
                }
            for(int j=m-1;j>i;j--)
                while(free[j] > 0 and vec[i]<k){
                    ans += (i-j+m)%m;
                    vec[i]++;
                    free[j]--;
                }
        }
    }

    cout << ans << endl;

    stack<s> st;
    int tt = 2;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                matrix[i].push_back({hell.loc,hell.val + kk});
            }
        }
    }

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 0;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;

    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    int frrr = 0;
    int ops = 0;
    int opz = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k){
            free[i]+=vec[i]-k;
            frrr += vec[i] - k;
        }
        else if(vec[i] < k and frrr > 0) {
            for(int j=i-1;j>=0;j--){
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                opz++;
                if(vec[i] >= k) break;
            }
            if(vec[i] >= k) break;
            for(int j=m-1;j>i;j--) {
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                opz++;
                if(vec[i] >= k) break;
            }
        }
    }

    stack<s> st;
    int tt = 2;
    ans = 0;
    int opss = 0;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                ans += kk;
                matrix[i].push_back({hell.loc,hell.val + kk});
                opss++;
            }
        }
    }
    if(n == 200000) debug = 1;
    dout << ops<< " " << opz << " " << opss << endl;
    cout << ans << endl;

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 1;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;
    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k)
            free[i]+=vec[i]-k;
        else {
            for(int j=i-1;j>=0;j--){
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                }
                if(vec[i] >= k) break;
            }
            for(int j=m-1;j>i;j--) {
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                }
                if(vec[i] >= k) break;
            }
        }
    }
    cout << ans << endl;

    stack<s> st;
    int tt = 2;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                matrix[i].push_back({hell.loc,hell.val + kk});
            }
        }
    }

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 0;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;

    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    int frrr = 0;
    int ops = 0;
    int opz = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k){
            free[i]+=vec[i]-k;
            frrr += vec[i] - k;
        }
        else if(vec[i] < k and frrr > 0) {
            for(int j=i-1;j>=0;j--){
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                opz++;
                if(vec[i] >= k) break;
            }
            if(vec[i] >= k) break;
            for(int j=m-1;j>i;j--) {
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                opz++;
                if(vec[i] >= k) break;
            }
        }
    }

    stack<s> st;
    int tt = 2;
    ans = 0;
    int opss = 0;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                ans += kk;
                matrix[i].push_back({hell.loc,hell.val + kk});
                opss++;
            }
        }
    }
    //if(n == 200000) debug = 1;
    dout << ops<< " " << opz << " " << opss << endl;
    cout << ans << endl;

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 0;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
#define int ulli

struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;

    vector<int> vec(m,0);
    vector<int> free(m,0);
    vector< vector<s> > matrix(m);

    for(int i=0;i<n;i++){
        int temp;
        cin>>temp;
        vec[temp%m]++;
        matrix[temp%m].push_back({i,temp});
    }

    int ans = 0;
    int frrr = 0;
    int ops = 0;
    for(int tt = 0;tt<2;tt++)
    for(int i=0;i<m;i++){
        if(vec[i] > k){
            free[i]+=vec[i]-k;
            frrr += vec[i] - k;
        }
        else if(vec[i] < k and frrr > 0) {
            for(int j=i-1;j>=0;j--){
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                if(vec[i] >= k) break;
            }
            if(vec[i] >= k) break;
            for(int j=m-1;j>i;j--) {
                if(vec[i] >= k) break;
                while(free[j] > 0 and vec[i]<k){
                    int hell = (i-j+m)%m;
                    int go = 0;
                    if(free[j] >= k-vec[i]){
                        go = k - vec[i];
                    } else {
                        go = free[j];
                    }
                    ans += hell*go;
                    vec[i]+=go;
                    free[j]-=go;
                    frrr -= go;
                    ops ++;
                }
                if(vec[i] >= k) break;
            }
        }
    }

    stack<s> st;
    int tt = 2;
    ans = 0;
    int opss = 0;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = (i- hell.val%m + m)%m;
                ans += kk;
                matrix[i].push_back({hell.loc,hell.val + kk});
                opss++;
            }
        }
    }
    if(n == 200000) debug = 1;
    dout << ops << " " << opss << endl;
    cout << ans << endl;

    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
        }

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

    cout << endl;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    while(t--)solve();
}


#include<bits/stdc++.h>
#define dout if(debug) cout<<" "

using namespace std;

int debug = 1;

typedef long long int ulli;
typedef unsigned int ui;
typedef pair<int,int> mp;
typedef vector<vector<int> > matrix;
#define int ulli

const int mod = 1e9+7;
struct s{
    int loc;
    int val;
};

void solve(){
    int n,m,k;
    cin>>n>>m;
    k = n/m;

    vector< vector<s> > matrix(m);

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

    stack<s> st;
    int ans = 0;
    int tt = 2;
    while(tt--){
        for(int i=0;i<m;i++){
            while(matrix[i].size() > (size_t)k){
                s hell = matrix[i].back();
                matrix[i].pop_back();
                st.push(hell);
            }
            while(matrix[i].size() < (size_t)k and st.size() > 0){
                s hell = st.top();
                st.pop();
                int kk = i-hell.val%m;
                ans += kk;
                matrix[i].push_back({hell.loc,hell.val + kk});
            }
        }
    }

    cout << ans << endl;
    vector<int> arr(n);
    for(int i=0;i<m;i++)
        for(size_t j=0;j<matrix[i].size();j++){
            s hell = matrix[i][j];
            arr[hell.loc] = hell.val;
            //dout << i << " " << j << " " << hell.loc << " " << hell.val << endl;
        }

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

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--)solve();
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k = abs(ii - fre.back().second);
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k = ii - fre.back().second;
			if (k < 0) {
				k = m - fre.back().second + i;
			}
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

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

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

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k;
			if (ii - fre.back().second >= 0) {
				k = ii - fre.back().second;
			}
			else {
				k = m - (ii - fre.back().second);
			}
			k %= m;
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
vector<vector<int>> g;
vector<bool> reach;
vector<bool> used;
vector<int> comp;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	ll n, m;
	cin >> n >> m;
	vector<ll> v;
	vector<vector<pair<int,int>>> mods(m,vector<pair<int,int>>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		mods[x%m].pb({ x,i });
		v.pb(x);
	}
	ll needed = n / m;
	ll over = 0;
	ll done = 0;
	//если c i уже = n/m, то с ним ничего делать не нужно(необязательно)
	for (int i = 0; i < m; i++) {
		int j = (i + 1) % m;
		int dis = 1;
		while (mods[i].size() > needed) {
			while (mods[i].size() > needed&&mods[j].size() < needed) {
				auto x = mods[i].back();
				mods[i].pop_back();
				done += dis;
				mods[j].pb({ x.first+dis,x.second });
			}
			j = (j + 1) % m;
			dis++;
		}
	}
	for (auto vv : mods) {
		for (auto ev : vv) {
			v[ev.second] = ev.first;
		}
	}
	cout << done << endl;
	for (auto ev : v)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	fori(m) {
		while (mod[i].size() > need) {
			fre.pb({ mod[i].back(),i });
			mod[i].pop_back();
		}
	}
	ll act = 0;
	for (ll i = m - 1; i > -1; i--) {
		while (mod[i].size() < need) {
			mod[i].pb(0LL);
			ll k = i - fre.back().second;
			if (k < 0) {
				k = m - fre.back().second + i;
			}
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k;
			if (ii - fre.back().second >= 0) {
				k = ii - fre.back().second;
			}
			else {
				k = m - fre.back().second + ii;
			}
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	fori(m) {
		while (mod[i].size() > need) {
			fre.pb({ mod[i].back(),i });
			mod[i].pop_back();
		}
	}
	ll act = 0;
	for (ll i = m - 1; i > -1; i--) {
		while (mod[i].size() < need) {
			mod[i].pb(0LL);
			act += i - fre.back().second;
			a[fre.back().first] += (i - fre.back().second);
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k;
			if (ii - fre.back().second >= 0) {
				k = ii - fre.back().second;
			}
			else {
				k = m - 1 - fre.back().second + ii;
			}
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k = abs(i - fre.back().second);
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k;
			if (ii - fre.back().second >= 0) {
				k = ii - fre.back().second;
			}
			else {
				k = m - ii + fre.back().second;
			}
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	ll act = 0;
	fori(2*m) {
		ll ii = i % m;
		while (mod[ii].size() > need) {
			fre.pb({ mod[ii].back(),ii });
			mod[ii].pop_back();
		}
		while (mod[ii].size() < need&&!fre.empty()) {
			mod[ii].pb(0LL);
			ll k = ii - fre.back().second;
			act += k;
			a[fre.back().first] += k;
			fre.pop_back();
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define fori(n) for (ll i=0;i<n;i++)
#define forj(n) for (ll j=0;j<n;j++)
#define pb push_back
ll n, m;
vector<ll> a;
vector<pair<ll, ll>> fre;//index and modulo
vector<vector<ll>> mod;
int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	cin >> n >> m;
	mod.resize(m, vector<ll>());
	fori(n) {
		ll x;
		scanf("%lld", &x);
		a.pb(x);
		mod[x%m].pb(i);
	}
	ll need = n / m;
	fori(m) {
		while (mod[i].size() > need) {
			fre.pb({ mod[i][mod[i].size()-1],i });
			mod[i].pop_back();
		}
	}
	ll act = 0;
	for (ll i = m - 1; i > -1; i--) {
		while (mod[i].size() < need) {
			mod[i].pb(0LL);
			act += i - fre.back().second;
			a[fre.back().first] += (i - fre.back().second);
		}
	}
	cout << act << endl;
	for (auto ev : a)
		cout << ev << ' ';
	return 0;
}


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2],r[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;
	int num[m],pos[m];

	int i;
	memset(pos,0,sizeof(int)*m);
	memset(num,0,sizeof(int)*m);
	memset(r,-1,sizeof(int)*maxn);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		r[i]=x[i]%m;
		num[r[i]]++;
		if(num[r[i]]>k&&pos[r[i]]==0)
		pos[r[i]]=i;
	}

	int maxr=0,manum=0;
	for(i=0;i<m;i++)
	if(manum<num[i])
	{
		maxr=i;
		manum=max(manum,num[i]);
	}

	/*printf("%d\n",maxr);
	*/
	int ans=0;

	vector < pair < int,int > > fre;
	for(i=0;i<2*m;i++){
		int p=i%m;
		int count=0;
		int j;

		if(num[p]>k){
			for(j=n;num[p]>k;j--){
				if(r[j]==p){
					fre.push_back(make_pair(j,p));
					num[p]--;
					/*r[j]=(r[j]+1)%m;
					num[p]--;
					num[(p+1)%m]++;
					ans++;
					x[j]++;
					*/
				}
			}
		}
		else{
			while( num [p] < k && !fre.empty() ){
				int elm=fre.back().first;
				int mm=fre.back().second;
				fre.pop_back();
				x[elm]+=p-mm;
				r[elm]+=p-mm;
				ans+=p-mm;
				num[p]++;
			}
		}
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2],r[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;
	int num[m],pos[m];

	int i;
	memset(pos,0,sizeof(int)*m);
	memset(num,0,sizeof(int)*m);
	memset(r,-1,sizeof(int)*maxn);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		r[i]=x[i]%m;
		num[r[i]]++;
		if(num[r[i]]>k&&pos[r[i]]==0)
		pos[r[i]]=i;
	}

	int maxr=0,manum=0;
	for(i=0;i<m;i++)
	if(manum<num[i])
	{
		maxr=i;
		manum=max(manum,num[i]);
	}

	/*printf("%d\n",maxr);
	*/
	int ans=0;

	vector < pair < int,int > > fre;
	for(i=0;i<2*m;i++){
		int p=i%m;
		int count=0;
		int j;

		if(num[p]>k){
			for(j=n;num[p]>k;j--){
				if(r[j]==p){
					fre.push_back(make_pair(j,p));
					num[p]--;
					/*r[j]=(r[j]+1)%m;
					num[p]--;
					num[(p+1)%m]++;
					ans++;
					x[j]++;
					*/
				}
			}
		}
		else{
			while( num [p] < k && !fre.empty() ){
				int elm=fre.back().first;
				int mm=fre.back().second;
				fre.pop_back();
				x[elm]+=p-mm;
				r[elm]=(r[elm]+p-mm)%m;
				ans+=p-mm;
				num[p]++;
			}
		}
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;

	int i;
	vector< vector<int> > val(m);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

	/*printf("%d\n",maxr);
	*/
	int ans=0;

	for(i=0;i<2*m;i++){
		int p=i%m;
		int count=0;
		int j;

		if(val[p].size()>k){
			while(val[p].size()>k){
				int elm=val[p].back();
				val[p].pop_back();
				x[elm]++;
				ans++;
				val[(p+1)%m].push_back(elm);
			}
		}
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int maxn=1e5+5;
int x[maxn];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;

	int i,j;
	vector <vector<int> > val(m);
	for(i=0;i<n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

	vector <pair<int,int> > fre;
	long long ans=0;
	for(i=0;i<2*m;i++){
		int cur=i%m;
		while(val[cur].size()>k){
			int elm=val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elm,i));
		}

		while(val[cur].size()<k && !fre.empty()){
			int elm=fre.back().first;
			int mm=fre.back().second;
			fre.pop_back();
			ans+=i-mm;
			x[elm]+=i-mm;
			val[cur].push_back(elm);
		}
	}

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


#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	long long ans = 0;
	vector< pair < int, int > > fre;

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

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

	return 0;
}


#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

const int maxn=2*1e5+5;
int x[maxn];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;
	int i,j;

	vector <vector< int> > val(m);

	for(i=0;i<n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

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

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;

	int i;
	vector< vector<int> > val(m);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

	/*printf("%d\n",maxr);
	*/
	long long ans=0;
	vector< pair<int,int> > fre;
	for(i=0;i<2*m;i++){
		int p=i%m;
		int count=0;
		int j;
		while(val[p].size()>k){
			int elm=val[p].back();
			val[p].pop_back();
			fre.push_back(make_pair(elm,i));
		}
		while(val[p].size()<k && !fre.empty()){
			int elm=fre.back().first;
			int mm=fre.back().second;
			ans+=i-mm;
			x[elm]+=i-mm;
			fre.pop_back();
			val[p].push_back(elm);
		}
		/*if(val[p].size()>k){
			while(val[p].size()>k){
				int elm=val[p].back();
				val[p].pop_back();
				x[elm]++;
				ans++;
				val[(p+1)%m].push_back(elm);
			}
		}*/
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2],r[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;
	int num[m],pos[m];

	int i;
	memset(pos,0,sizeof(int)*m);
	memset(num,0,sizeof(int)*m);
	memset(r,-1,sizeof(int)*maxn);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		r[i]=x[i]%m;
		num[r[i]]++;
		if(num[r[i]]>k&&pos[r[i]]==0)
		pos[r[i]]=i;
	}

	int maxr=0,manum=0;
	for(i=0;i<m;i++)
	if(manum<num[i])
	{
		maxr=i;
		manum=max(manum,num[i]);
	}

	printf("%d\n",maxr);

	int ans=0;

	for(i=maxr;i<maxr+m;i++){
		int p=i%m;
		int count=0;
		int j;

		if(num[p]>k){
			for(j=n;num[p]>k;j--){
				if(r[j]==p){
					r[j]=(r[j]+1)%m;
					num[p]--;
					num[(p+1)%m]++;
					ans++;
					x[j]++;
				}
			}
		}
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=2*1e5+5;

int x[maxn];

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

	vector<pair<int,int> > fre;
	long long ans=0;
	for(i=0;i<2*m;i++){
		int cur=i%m;
		while(val[cur].size()>k){
			int elm=val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elm,i));
		}

		while(val[cur].size()<k&& !fre.empty()){
			int elm=fre.back().first;
			int mm=fre.back().second;
			fre.pop_back();
			ans+=i-mm;
			x[elm]+=i-mm;
			val[cur].push_back(elm);
		}
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;

	int i;
	vector< vector<int> > val(m);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

	/*printf("%d\n",maxr);
	*/
	int ans=0;
	vector< pair<int,int> > fre;
	for(i=0;i<2*m;i++){
		int p=i%m;
		int count=0;
		int j;
		while(val[p].size()>k){
			int elm=val[p].back();
			val[p].pop_back();
			fre.push_back(make_pair(elm,i));
		}
		while(val[p].size()<k && !fre.empty()){
			int elm=fre.back().first;
			int mm=fre.back().second;
			ans+=i-mm;
			x[elm]+=i-mm;
			fre.pop_back();
			val[p].push_back(elm);
		}
		/*if(val[p].size()>k){
			while(val[p].size()>k){
				int elm=val[p].back();
				val[p].pop_back();
				x[elm]++;
				ans++;
				val[(p+1)%m].push_back(elm);
			}
		}*/
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;

	int i;
	vector< vector<int> > val(m);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

	/*printf("%d\n",maxr);
	*/
	long long ans=0;
	vector< pair<int,int> > fre;
	for(i=0;i<2*m;i++){
		int p=i%m;
		int count=0;
		int j;
		while(val[p].size()>k){
			int elm=val[p].back();
			val[p].pop_back();
			fre.push_back(make_pair(elm,i));
		}
		while(val[p].size()<k && !fre.empty()){
			int elm=fre.back().first;
			int mm=fre.back().second;
			ans+=i-mm;
			x[elm]+=i-mm;
			fre.pop_back();
			val[p].push_back(elm);
		}
		/*if(val[p].size()>k){
			while(val[p].size()>k){
				int elm=val[p].back();
				val[p].pop_back();
				x[elm]++;
				ans++;
				val[(p+1)%m].push_back(elm);
			}
		}*/
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int maxn=2*1e5+5;
int x[maxn];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;

	int i,j;
	vector < vector <int> > val(m);
	for(i=0;i<n;i++){
		scanf("%d",&x[i]);
		val[x[i]%m].push_back(i);
	}

	vector < pair<int,int> > fre;
	long long ans=0;
	for(i=0;i<2*m;i++){
		int cur=i%m;
		while(val[cur].size()>k){
			int elm=val[cur].back();
			val[cur].pop_back();
			fre.push_back(make_pair(elm,i));
		}

		while(val[cur].size()<k && !fre.empty()){
			int elm=fre.back().first;
			int mm=fre.back().second;
			fre.pop_back();
			ans+=i-mm;
			x[elm]+=i-mm;
			val[cur].push_back(elm);
		}
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2],r[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;
	int num[m],pos[m];

	int i;
	memset(pos,0,sizeof(int)*m);
	memset(num,0,sizeof(int)*m);
	memset(r,-1,sizeof(int)*maxn);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		r[i]=x[i]%m;
		num[r[i]]++;
		if(num[r[i]]>k&&pos[r[i]]==0)
		pos[r[i]]=i;
	}

	int maxr=0,manum=0;
	for(i=0;i<m;i++)
	if(manum<num[i])
	{
		maxr=i;
		manum=max(manum,num[i]);
	}

	/*printf("%d\n",maxr);
	*/
	int ans=0;

	vector < pair < int,int > > fre;
	for(i=maxr;i<maxr+m;i++){
		int p=i%m;
		int count=0;
		int j;

		if(num[p]>k){
			for(j=n;num[p]>k;j--){
				if(r[j]==p){
					fre.push_back(make_pair(j,p));
					num[p]--;
					/*r[j]=(r[j]+1)%m;
					num[p]--;
					num[(p+1)%m]++;
					ans++;
					x[j]++;
					*/
				}
			}
		}
		else{
			while(num[p]<k){
				int elm=fre.back().first;
				int mm=fre.back().second;
				fre.pop_back();
				x[elm]+=(p+m-mm)%m;
				r[elm]+=(p+m-mm)%m;
				ans+=(p+m-mm)%m;
				num[p]++;
			}
		}
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;

int maxn=2*100000+2;
int x[2*100000+2],r[2*100000+2];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=n/m;
	int num[m],pos[m];

	int i;
	memset(pos,0,sizeof(int)*m);
	memset(num,0,sizeof(int)*m);
	memset(r,-1,sizeof(int)*maxn);
	for(i=1;i<=n;i++){
		scanf("%d",&x[i]);
		r[i]=x[i]%m;
		num[r[i]]++;
		if(num[r[i]]>k&&pos[r[i]]==0)
		pos[r[i]]=i;
	}

	int maxr=0,manum=0;
	for(i=0;i<m;i++)
	if(manum<num[i])
	{
		maxr=i;
		manum=max(manum,num[i]);
	}

	/*printf("%d\n",maxr);
	*/
	int ans=0;

	for(i=maxr;i<maxr+m;i++){
		int p=i%m;
		int count=0;
		int j;

		if(num[p]>k){
			for(j=n;num[p]>k;j--){
				if(r[j]==p){
					r[j]=(r[j]+1)%m;
					num[p]--;
					num[(p+1)%m]++;
					ans++;
					x[j]++;
				}
			}
		}
		/*else
		count=num[p];
		*/
		/*for(j=1;j<=n&&count<=k;j++){
			if(r[j]==p){
				if(count<k)
				count++;
				else
				pos[p]=j;
			}
		}
		*/
	}

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


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
vector<int>st[200002];
int ind[200002];

int main()
{
    int n,i,j,m,rs,val,lp,a,d,mn;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].push_back(i);
    }
    ans=0,a=-1,lp=0;
    while(lp<2)
    {
        for(i=0; i<m; i++)
        {
            if(cnt[i]<d)
            {
                if(a!=-1)
                {
                    mn=min(cnt[a]-d,d-cnt[i]);
                    for(j=ind[a]; mn>0; mn--,j++)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                    }
                    ind[a]=j;
                    if(cnt[a]==d)a=-1,st[a].clear();
                }
            }
            else if(cnt[i]>d)
            {
                if(a==-1)a=i;
                else
                {
                    mn=cnt[a]-d;
                    for(j=st[a].size()-1; mn>0; mn--,j--)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                        st[i].push_back(val);
                    }
                    st[a].clear(),a=i;
                }
            }
            else st[i].clear();
        }
            lp++;
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
vector<int>st[200002];
int ind[200002];

int main()
{
    int n,i,j,m,rs,val,lp,a,d,mn;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].push_back(i);
    }
    ans=0,a=-1,lp=0;
    while(lp<2)
    {
        for(i=0; i<m; i++)
        {
            if(cnt[i]<d)
            {
                if(a!=-1)
                {
                    mn=min(cnt[a]-d,d-cnt[i]);
                    for(j=ind[a]; mn>0; mn--,j++)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                    }
                    ind[a]=j;
                    if(cnt[a]==d)a=-1,st[a].clear();
                    if(cnt[i]==d)st[i].clear();
                }
            }
            else if(cnt[i]>d)
            {
                if(a==-1)a=i;
                else
                {
                    mn=cnt[a]-d;
                    for(j=st[a].size()-1; mn>0; mn--,j--)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                        st[i].push_back(val);
                    }
                    st[a].clear(),a=i;
                }
            }
            else st[i].clear();
        }
        lp++;
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
set<int>st[200002];
queue<int>besi,kom;

int main()
{
    int n,i,j,m,rs,a,d;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].insert(i);
    }
    ans=0;
    for(i=0; i<m; i++)
    {
        if(cnt[i]>d)besi.push(i);
        else if(cnt[i]<d)kom.push(i);
    }
    while(!besi.empty())
    {
        i=besi.front();
        a=cnt[i]-d;
        rs=kom.front();
        besi.pop();
        if(d-cnt[rs]>a)cnt[rs]+=a;
        else
        {
            auto it=st[i].begin();
            while(a>0)
            {
                j=min((d-cnt[rs]),a),a-=j;
                for(; j>0; j--,it++)
                {
                    if(rs>i)
                    {
                        ara[*it]=ara[*it]+(rs-i);
                        ans+=(rs-i);
                    }
                    else
                    {
                        if(i!=0)ans+=(m-i),ara[*it]+=(m-i);
                        ara[*it]=ara[*it]+rs,ans+=rs;
                    }
                    cnt[rs]++;
                }
                if(cnt[rs]==d)kom.pop();
                if(!kom.empty())rs=kom.front();
            }
        }
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
set<int>st[200002];
queue<int>besi,kom;

int main()
{
    int n,i,j,m,rs,a,d,ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].insert(i);
    }
    ans=0;
    for(i=0; i<m; i++)
    {
        if(cnt[i]>d)besi.push(i);
        else if(cnt[i]<d)kom.push(i);
    }
    while(!besi.empty())
    {
        i=besi.front();
        a=cnt[i]-d;
        rs=kom.front();
        besi.pop();
        if(d-cnt[rs]>a)cnt[rs]+=a;
        else
        {
            auto it=st[i].begin();
            while(a>0)
            {
                j=min((d-cnt[rs]),a),a-=j;
                for(; j>0; j--,it++)
                {
                    if(rs>i)
                    {
                        ara[*it]=ara[*it]+(rs-i);
                        ans+=(rs-i);
                    }
                    else
                    {
                        if(i!=0)ans+=(m-i);
                        ara[*it]=0;
                        ara[*it]=ara[*it]+rs,ans+=rs;
                    }
                    cnt[rs]++;
                }
                if(cnt[rs]==d)kom.pop();
                if(!kom.empty())rs=kom.front();
            }
        }
    }
    printf("%d\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
set<int>st[200002];
queue<int>besi;
vector<int>kom;

int main()
{
    int n,i,j,m,rs,a,d;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].insert(i);
    }
    ans=0;
    for(i=0; i<m; i++)
    {
        if(cnt[i]>d)besi.push(i);
        else if(cnt[i]<d)kom.push_back(i);
    }
    while(!besi.empty())
    {
        i=besi.front();
        a=cnt[i]-d;
        besi.pop();
        auto it=st[i].begin();
        while(a>0)
        {
            if(i<kom[kom.size()-1])
            {
                rs=upper_bound(kom.begin(),kom.end(),i)-kom.begin();
            }
            else rs=0;
            j=min((d-cnt[kom[rs]]),a),a-=j;
            for(; j>0; j--,it++)
            {
                if(kom[rs]>i)
                {
                    ara[*it]=ara[*it]+(kom[rs]-i);
                    ans+=(kom[rs]-i);
                }
                else
                {
                    if(i!=0)ans+=(m-i),ara[*it]+=(m-i);
                    ara[*it]=ara[*it]+kom[rs],ans+=kom[rs];
                }
                cnt[kom[rs]]++;
            }
            if(cnt[kom[rs]]==d)
            {
               kom.erase(kom.begin()+rs);
            }
        }
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
set<int>st[200002];
queue<int>besi;
vector<int>kom;

int main()
{
    int n,i,j,m,rs,a,d;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].insert(i);
    }
    ans=0;
    for(i=0; i<m; i++)
    {
        if(cnt[i]>d)besi.push(i);
        else if(cnt[i]<d)kom.push_back(i);
    }
    while(!besi.empty())
    {
        i=besi.front();
        a=cnt[i]-d;
        besi.pop();
        auto it=st[i].begin();
        while(a>0)
        {
            if(a<kom[kom.size()-1])
            {
                rs=upper_bound(kom.begin(),kom.end(),i)-kom.begin();
            }
            else rs=0;
            j=min((d-cnt[kom[rs]]),a),a-=j;
            for(; j>0; j--,it++)
            {
                if(kom[rs]>i)
                {
                    ara[*it]=ara[*it]+(kom[rs]-i);
                    ans+=(kom[rs]-i);
                }
                else
                {
                    if(i!=0)ans+=(m-i),ara[*it]+=(m-i);
                    ara[*it]=ara[*it]+kom[rs],ans+=kom[rs];
                }
                cnt[kom[rs]]++;
            }
            if(cnt[kom[rs]]==d)
            {
               kom.erase(kom.begin()+rs);
            }
        }
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
vector<int>st[200002];
int ind[200002];

int main()
{
    int n,i,j,m,rs,val,lp,a,d,mn;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].push_back(i);
    }
    ans=0,a=-1,lp=0;
    while(lp<2)
    {
        for(i=0; i<m; i++)
        {
            if(cnt[i]<d)
            {
                if(a!=-1)
                {
                    mn=min(cnt[a]-d,d-cnt[i]);
                    for(j=ind[a]; mn>0; mn--,j++)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                    }
                    ind[a]=j;
                    if(cnt[a]==d)a=-1,st[a].clear();
                }
            }
            else if(cnt[i]>d)
            {
                if(a==-1)a=i;
                else
                {
                    mn=cnt[a]-d;
                    for(j=st[a].size()-1; mn>0; mn--,j--)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                        st[i].push_back(val);
                    }
                    st[a].clear(),a=i;
                }
            }
            else st[i].clear();
            lp++;
        }
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
vector<int>st[200002];
queue<int>besi;
vector<int>kom;

int main()
{
    int n,i,j,m,rs,val,lp,a,d,mn;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].push_back(i);
    }
    ans=0,a=-1,lp=0;
    while(lp<2)
    {
        for(i=0; i<m; i++)
        {
            if(cnt[i]<d)
            {
                if(a!=-1)
                {
                    mn=min(cnt[a]-d,d-cnt[i]);
                    for(j=0; j<mn; j++)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                    }
                    if(cnt[a]==d)a=-1,st[a].clear();
                }
            }
            else if(cnt[i]>d)
            {
                if(a==-1)a=i;
                else
                {
                    mn=cnt[a]-d;
                    for(j=st[a].size()-1; mn>0; mn--,j--)
                    {
                        val=st[a][j];
                        if(i>a)ara[val]+=(i-a),ans+=(i-a);
                        else
                        {
                            ara[val]+=(m-a+i),ans+=(m-a+i);
                        }
                        cnt[a]--,cnt[i]++;
                        st[i].push_back(val);
                    }
                    st[a].clear(),a=i;
                }
            }
            else st[i].clear();
            lp++;
        }
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int> pi;
int ara[200002],cnt[200002];
set<int>st[200002];
queue<int>besi,kom;

int main()
{
    int n,i,j,m,rs,a,d;
    ll ans;
    cin>>n>>m;
    d=n/m;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
        a=ara[i]%m,cnt[a]++;
        st[a].insert(i);
    }
    ans=0;
    for(i=0; i<m; i++)
    {
        if(cnt[i]>d)besi.push(i);
        else if(cnt[i]<d)kom.push(i);
    }
    while(!besi.empty())
    {
        i=besi.front();
        a=cnt[i]-d;
        rs=kom.front();
        besi.pop();
        if(d-cnt[rs]>a)cnt[rs]+=a;
        else
        {
            auto it=st[i].begin();
            while(a>0)
            {
                j=min((d-cnt[rs]),a),a-=j;
                for(; j>0; j--,it++)
                {
                    if(rs>i)
                    {
                        ara[*it]=ara[*it]+(rs-i);
                        ans+=(rs-i);
                    }
                    else
                    {
                        if(i!=0)ans+=(m-i);
                        ara[*it]=0;
                        ara[*it]=ara[*it]+rs,ans+=rs;
                    }
                    cnt[rs]++;
                }
                if(cnt[rs]==d)kom.pop();
                if(!kom.empty())rs=kom.front();
            }
        }
    }
    printf("%lld\n",ans);
    for(i=1; i<=n; i++)printf("%d ",ara[i]);
    printf("\n");
    return 0;
}


#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back

const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const ll inf = -1e18;
const ll M = 5 * 1e8;

ll n, d;
ll a[N], y[N], x[N], dp[110][110];

int main()
{
	cin >> n >> d;
	for (int i = 2; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i] >> y[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			dp[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]) * d;
			dp[i][j] -= a[i];
		}
		dp[i][i] = 0;
	}
	for (int k = 1; k <= n; k++)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (dp[i][j] > dp[i][k] + dp[k][j])
				{
					dp[i][j] = dp[i][k] + dp[k][j];
				}
			}
		}
	}
	cout << dp[1][n];
	cin.get(), cin.get();
	return 0;
}


#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back

const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const ll inf = -1e18;
const ll M = 5 * 1e8;

ll n, d;
ll a[N], y[N], x[N], dp[110][110];

int main()
{
	cin >> n >> d;
	for (int i = 2; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i] >> y[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			dp[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]) * d;
			dp[i][j] -= a[j];
		}
		dp[i][i] = 0;
	}
	for (int k = 1; k <= n; k++)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (dp[i][j] > dp[i][k] + dp[k][j])
				{
					dp[i][j] = dp[i][k] + dp[k][j];
				}
			}
		}
	}
	cout << dp[1][n];
	cin.get(), cin.get();
	return 0;
}


#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back

const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const ll inf = -1e18;
const ll M = 5 * 1e8;

ll n, d;
ll a[N], y[N], x[N], dp[110][110];

int main()
{
	cin >> n >> d;
	for (int i = 2; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i] >> y[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			dp[i][j] = (abs(x[i] - x[j]) + abs(y[i] - y[j])) * d;
			dp[i][j] -= a[i];
		}
		dp[i][i] = 0;
	}
	//for (int i = 1; i <= n; i++)
	//{
	//	for (int j = 1; j <= n; j++)
	//	{
	//		cout << dp[i][j] << " ";
	//	}
	//	cout << '\n';
	//}
	for (int k = 1; k <= n; k++)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (dp[i][j] > dp[i][k] + dp[k][j])
				{
					dp[i][j] = dp[i][k] + dp[k][j];
				}
			}
		}
	}
	cout << dp[1][n];
	cin.get(), cin.get();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ll long long
const int MX=200010;
int N, M, k, d;
int arr[MX];
int calc(int x, int y, int xx, int yy)
{
    int a=abs(x-xx)+abs(y-yy);
    return a*d;
}
set<pair<ll, pair<int, int> > > st;
ll D[400][400];
int x[400], y[400];
int main()
{
    cin>>N>>d;
    for(int i=2;i<N;i++) cin>>arr[i];

    for(int i=1;i<=N;i++) cin>>x[i]>>y[i];
    for(int i=0;i<400;i++) for(int j=0;j<400;j++) D[i][j]=1e18;
    D[x[1]+101][y[1]+101]=0;
    st.insert(mp(0, mp(x[1], y[1])));
    while(st.size())
    {
        pair<int, pair<int, int> > p=*st.begin();
        st.erase(p);
        if(p.F > D[p.S.F][p.S.S]) continue;
        for(int i=1;i<=N;i++)
        {
            if(x[i] == p.S.F && y[i] == p.S.S) continue;
            if(D[101+p.S.F][101+p.S.S]+calc(p.S.F, p.S.S, x[i], y[i]) - arr[i]< D[x[i]+101][y[i]+101])
            {
                D[x[i]+101][y[i]+101]=D[101+p.S.F][101+p.S.S]+calc(p.S.F, p.S.S, x[i], y[i])-arr[i];
                st.insert(mp(D[x[i]+101][y[i]+101], mp(x[i], y[i])));
            }
        }
    }
    cout<<D[x[N]+101][y[N]+101];
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ll long long
const int MX=200010;
int N, M, k, d;
int arr[MX];
ll calc(int x, int y, int xx, int yy)
{
    ll a=abs(x-xx)+abs(y-yy);
    return (ll)a*d;
}
set<pair<ll, pair<int, int> > > st;
ll D[400][400];
int x[400], y[400];
int main()
{
    cin>>N>>d;
    for(int i=2;i<N;i++) cin>>arr[i];

    for(int i=1;i<=N;i++) cin>>x[i]>>y[i];
    for(int i=0;i<400;i++) for(int j=0;j<400;j++) D[i][j]=1e18;
    D[x[1]+101][y[1]+101]=0;
    st.insert(mp(0, mp(x[1], y[1])));
    while(st.size())
    {
        pair<int, pair<int, int> > p=*st.begin();
        st.erase(p);
        if(p.F > D[p.S.F+101][p.S.S+101]) continue;
        for(int i=1;i<=N;i++)
        {
            if(x[i] == p.S.F && y[i] == p.S.S) continue;
            if(D[101+p.S.F][101+p.S.S]+calc(p.S.F, p.S.S, x[i], y[i]) - arr[i]< D[x[i]+101][y[i]+101])
            {
                D[x[i]+101][y[i]+101]=D[101+p.S.F][101+p.S.S]+calc(p.S.F, p.S.S, x[i], y[i])-arr[i];
                st.insert(mp(D[x[i]+101][y[i]+101], mp(x[i], y[i])));
            }
        }
    }
    cout<<D[x[N]+101][y[N]+101];
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ll long long
const int MX=200010;
int N, M, k, d;
int arr[MX];
int calc(int x, int y, int xx, int yy)
{
    int a=abs(x-xx)+abs(y-yy);
    return a*d;
}
set<pair<int, pair<int, int> > > st;
int D[400][400];
int x[400], y[400];
int main()
{
    cin>>N>>d;
    for(int i=2;i<N;i++) cin>>arr[i];

    for(int i=1;i<=N;i++) cin>>x[i]>>y[i];
    for(int i=0;i<400;i++) for(int j=0;j<400;j++) D[i][j]=1e8;
    D[x[1]+101][y[1]+101]=0;
    st.insert(mp(0, mp(x[1], y[1])));
    while(st.size())
    {
        pair<int, pair<int, int> > p=*st.begin();
        st.erase(p);
        if(p.F > D[p.S.F][p.S.S]) continue;
        for(int i=1;i<=N;i++)
        {
            if(x[i] == p.S.F && y[i] == p.S.S) continue;
            if(D[101+p.S.F][101+p.S.S]+calc(p.S.F, p.S.S, x[i], y[i]) - arr[i]< D[x[i]+101][y[i]+101])
            {
                D[x[i]+101][y[i]+101]=D[101+p.S.F][101+p.S.S]+calc(p.S.F, p.S.S, x[i], y[i])-arr[i];
                st.insert(mp(D[x[i]+101][y[i]+101], mp(x[i], y[i])));
            }
        }
    }
    cout<<D[x[N]+101][y[N]+101];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=105;
int p[N],mp[N][N];
int n,d;
int abs(int a){
    return a<0?-a:a;
}
struct node{
    int x,y;
}po[N];
int getcost(node a,node b){
    int dis=abs(a.x-b.x)+abs(a.y-b.y);
    return dis*d;
}
int main()
{
    while(cin>>n>>d){
         memset(p,0,sizeof(p));
         for(int i=2;i<n;i++){
             scanf("%d",&p[i]);  //以i为起点的路线price减p[i];
         }
         for(int i=1;i<=n;i++){
             scanf("%d%d",&po[i].x,&po[i].y);
             for(int j=1;j<i;j++){ //下标的艺术！！
                 int cost=getcost(po[j],po[i]);
                 mp[j][i]=cost-p[j];
                 mp[i][j]=cost-p[i];
             }
         }
         for(int k=1;k<=n;k++){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
                }
            }
         }
         int ans=mp[1][n];
         if(ans<0) ans=0;
         printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#include <climits>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 998244353;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
const long long N = 1e2 + 10;

int n , d;
ll a[N] , dist[N] , par[N];
pii cor[N];
vector<pii> adj[N];

void dijkstra(int src)
{
	for (int i = 0;i<n;i++) {
		dist[i] = inf;
		par[i] = -1;
	}
	set<pii> s;
	dist[src] = 0;
	s.insert({0 , src});
	while(!s.empty())
	{
		int v = (*s.begin()).second;
		s.erase(s.begin());
		for (int i = 0;i<adj[v].size() ;i++) {
			int u = adj[v][i].second;
			int w = adj[v][i].first;
			if(dist[u] > dist[v] + w) {
				if(s.find({dist[u] , u})!=s.end()) {
					s.erase(s.find({dist[u] , u}));
				}
				dist[u] = dist[v] + w;
				s.insert({dist[u] , u});
				par[u] = v;
			}
		}
	}
}

int main()
{
	faster();
	cin>>n>>d;
	for(int i = 1;i<n - 1;i++)
	{
		cin>>a[i];
	}
	for(int i = 0;i<n;i++)
	{
		cin>>cor[i].first>>cor[i].second;
		int x = cor[i].first;
		int y = cor[i].second;
		for(int j = 0;j < i;j++)
		{
			ll w = abs(cor[i].first - cor[j].first) + abs(cor[i].second - cor[j].second);
			w*=d;
			w-=a[j];
			adj[i].push_back({w , j});
			adj[j].push_back({w , i});
		}
	}
	dijkstra(0);
	cout<<dist[n - 1]<<endl;
}


#include <bits/stdc++.h>
#include <climits>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 998244353;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
const long long N = 1e2 + 10;

int n , d;
ll a[N] , dist[N];
pii cor[N];
bool visited[N];
vector<pii> adj[N];

void dijkstra(int src)
{
	for (int i = 0;i<n;i++) {
		dist[i] = inf;
		visited[i] = false;
	}
	set<pii> s;
	dist[src] = 0;
	s.insert({0 , src});
	while(!s.empty())
	{
		int v = (*s.begin()).second;
		s.erase(s.begin());
		if(visited[v]==true)
			continue;
		visited[v] = true;
		for (int i = 0;i<adj[v].size() ;i++) {
			int u = adj[v][i].second;
			int w = adj[v][i].first;
			if(visited[u]==false)
			{
				w -= a[u];
			}
			if(dist[u] > dist[v] + w) {
				if(s.find({dist[u] , u})!=s.end()) {
					s.erase(s.find({dist[u] , u}));
				}
				dist[u] = dist[v] + w;
				s.insert({dist[u] , u});
			}
		}
	}
}

int main()
{
	faster();
	cin>>n>>d;
	for(int i = 1;i<n - 1;i++)
	{
		cin>>a[i];
	}
	for(int i = 0;i<n;i++)
	{
		cin>>cor[i].first>>cor[i].second;
		int x = cor[i].first;
		int y = cor[i].second;
		for(int j = 0;j < i;j++)
		{
			ll w = abs(x - cor[j].first) + abs(y - cor[j].second);
			w*=d;
			adj[i].push_back({w , j});
			adj[j].push_back({w , i});
		}
	}
	dijkstra(0);
	cout<<dist[n - 1]<<endl;
}


#include<bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
using namespace std;

int n,d,arr[105];
int b,c;
int jar[105][105];
pair<int,int>pos[105];

int main()
{
	scanf("%d %d",&n,&d);
	for(int x=2;x<=n-1;x++) scanf("%d",&arr[x]);

	for(int x=1;x<=n;x++)
	{
		scanf("%d %d",&pos[x].f,&pos[x].s);
	}

	for(int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			jar[x][y]=(abs(pos[x].s-pos[y].s)+abs(pos[x].f-pos[y].f))*d-arr[y];
			//printf("asu %d %d %d\n",x,y,jar[x][y]);
		}
	}

	for(int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			for(int z=1;z<=n;z++)
			{
				jar[y][z]=min(jar[y][z],jar[y][x]+jar[x][z]);
			}
		}
	}
	printf("%d\n",jar[1][n]);
}


#include<bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
using namespace std;

int n,d,arr[105];
int b,c;
int jar[105][105];
pair<int,int>pos[105];

int main()
{
	scanf("%d %d",&n,&d);
	for(int x=2;x<=n-1;x++) scanf("%d",&arr[x]);

	for(int x=1;x<=n;x++)
	{
		scanf("%d %d",&pos[x].f,&pos[x].s);
	}

	for(int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			if(x==y) continue;
			jar[x][y]=(abs(pos[x].s-pos[y].s)+abs(pos[x].f-pos[y].f))*d-arr[y];
			//printf("asu %d %d %d\n",x,y,jar[x][y]);
		}
	}

	for(int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			for(int z=1;z<=n;z++)
			{
				jar[y][z]=min(jar[y][z],jar[y][x]+jar[x][z]);
			}
		}
	}
	printf("%d\n",jar[1][n]);
}


#include<bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
using namespace std;

int n,d,arr[105];
int b,c;
int jar[105][105];
pair<int,int>pos[105];

int main()
{
	scanf("%d %d",&n,&d);
	for(int x=2;x<=n-1;x++) scanf("%d",&arr[x]);

	for(int x=1;x<=n;x++)
	{
		scanf("%d %d",&pos[x].f,&pos[x].s);
	}

	for(int x=1;x<=n;x++)
	{
		for(int y=x+1;y<=n;y++)
		{
			jar[x][y]=(abs(pos[x].s-pos[y].s)+abs(pos[x].f-pos[y].f))*d-arr[y];
			//printf("asu %d %d %d\n",x,y,jar[x][y]);
		}
	}

	for(int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			for(int z=1;z<=n;z++)
			{
				jar[y][z]=min(jar[y][z],jar[y][x]+jar[x][z]);
			}
		}
	}
	printf("%d\n",jar[1][n]);
}


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

#define endl '\n'
#define REP(a,b) for(int a=0;a<(b);a++)
#define FOR(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define RFOR(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define ll long long
#define ii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front

const int MAXN=105;

struct gr{
	int x,y,a;
}g[MAXN];

int n,d;
int jarak[MAXN][MAXN];

int dist(gr a,gr b){
	return d*(abs(a.x-b.x)+abs(a.y-b.y));
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>d;
	FOR(i,2,n-1,1){
		cin>>g[i].a;
	}
	FOR(i,1,n,1){
		cin>>g[i].x>>g[i].y;
	}
	FOR(i,1,n,1){
		FOR(j,i+1,n,1){
			jarak[i][j]=dist(g[i],g[j])-g[i].a;
			jarak[j][i]=jarak[i][j];
		}
	}

	FOR(k,1,n,1){
		FOR(i,1,n,1){
			FOR(j,1,n,1){
				jarak[i][j]=min(jarak[i][j],jarak[i][k]+jarak[k][j]);
			}
		}
	}

	cout<<jarak[1][n]<<endl;
}


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

#define endl '\n'
#define REP(a,b) for(int a=0;a<(b);a++)
#define FOR(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define RFOR(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define ll long long
#define ii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front

const int MAXN=105;

struct gr{
	int x,y,a;
}g[MAXN];

int n,d;
int jarak[MAXN][MAXN];

int dist(gr a,gr b){
	return d*(abs(a.x-b.x)+abs(a.y-b.y));
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>d;
	FOR(i,2,n-1,1){
		cin>>g[i].a;
	}
	FOR(i,1,n,1){
		cin>>g[i].x>>g[i].y;
	}

	FOR(i,1,n,1){
		FOR(j,i+1,n,1){
			jarak[i][j]=dist(g[i],g[j])-g[i].a;
			jarak[j][i]=dist(g[i],g[j])-g[j].a;
		}
	}

	FOR(k,1,n,1){
		FOR(i,1,n,1){
			FOR(j,1,n,1){
				jarak[i][j]=min(jarak[i][j],jarak[i][k]+jarak[k][j]);
			}
		}
	}

	cout<<jarak[1][n]<<endl;
}


#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define ll long long
#define ld long double

pair<int, int> coor[102];
int a[102];

int dst(int a, int b)
{
    return abs(coor[a].x - coor[b].x) + abs(coor[a].y - coor[b].y);
}

ll f[110];

int main()
{
    //ios_base::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, d;
    cin >> n >> d;

    for (int i = 2 ; i < n ; i++)
        cin >> a[i];

    for (int i = 1 ; i <= n ; i++) {
        cin >> coor[i].x >> coor[i].y;
        f[i] = 1e18;
    }

    f[1] = 0;

    queue<int> q;
    q.push(1);

    while (q.size()) {
        int id = q.front();
        q.pop();

        for (int i = 2 ; i <= n ; i++) {
            if (i != id && f[id] + d * dst(id, i) - a[i] < f[i]) {
                f[i] = f[id] + d * dst(id, i) - a[i];
                q.push(i);
            }
        }
    }

    if (f[n] < 0)
        cout << 0;
    else
        cout << f[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,pair<int,set<int> > > > q;
bool used[101];
int dis[101];
set<int> st;
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, make_pair(1, st)));
    dis[1] = 0;
    while(!q.empty())
    {
        int v = q.top().second.first;
        int s = q.top().first;
        st = q.top().second.second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                set<int> t = st;
                dis[i] = min(dis[i], s + abs(x[i] - x[v]) + abs(y[i] - y[v]) * d);
                if(!t.count(i))
                {
                    dis[i] -= a[i];
                    t.insert(i);
                }
                q.push(make_pair(-1 * dis[i], make_pair(i, t)));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,pair<int,set<int> > > > q;
bool used[101];
int dis[101];
set<int> st;
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, make_pair(1, st)));
    dis[1] = 0;
    while(!q.empty())
    {
        int v = q.top().second.first;
        int s = q.top().first;
        st = q.top().second.second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                set<int> t = st;
                int h = -1 * s + (abs(x[i] - x[v]) + abs(y[i] - y[v])) * d;
                if(!t.count(i))
                {
                    h -= a[i];
                }
                if(dis[i] > h)
                {
                    dis[i] = h;
                    t.insert(i);
                }
                q.push(make_pair(-1 * dis[i], make_pair(i, t)));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,int> > q;
bool used[101];
int dis[101];
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, 1));
    while(!q.empty())
    {
        int v = q.top().second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                dis[i] = min(dis[i], abs(x[i] - x[v]) + abs(y[i] - y[v]) * d - a[i]);
                q.push(make_pair(-1 * dis[i], i));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,pair<int,set<int> > > > q;
bool used[101];
int dis[101];
set<int> st;
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, make_pair(1, st)));
    dis[1] = 0;
    while(!q.empty())
    {
        int v = q.top().second.first;
        int s = q.top().first;
        st = q.top().second.second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                set<int> t = st;
                dis[i] = min(dis[i], -1 * s + (abs(x[i] - x[v]) + abs(y[i] - y[v])) * d);
                if(!t.count(i))
                {
                    dis[i] -= a[i];
                    t.insert(i);
                }
                q.push(make_pair(-1 * dis[i], make_pair(i, t)));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,pair<int,set<int> > > > q;
bool used[101];
int dis[101];
set<int> st;
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, make_pair(1, st)));
    while(!q.empty())
    {
        int v = q.top().second.first;
        st = q.top().second.second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                set<int> t = st;
                dis[i] = min(dis[i], dis[v] + abs(x[i] - x[v]) + abs(y[i] - y[v]) * d);
                if(!t.count(i))
                {
                    dis[i] -= a[i];
                    t.insert(i);
                }
                q.push(make_pair(-1 * dis[i], make_pair(i, t)));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[1001];
int x[501], y[101];
bool used[101];
int d[101];
int p[101];
int funk(int i, int j)
{
    return abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
main()
{
    int n, m, i, j, s = 0;
    cin >> n >> m;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
    {
        cin >> x[i] >> y[i];
    }
    for(i = 1; i <= n; i ++)
        d[i] = 1e9;
    d[1] = 0;
    for(i = 1; i <= n; i ++)
    {
        int v, ss = 1e9;
        for(j = 1; j <= n; j ++)
        {
            if(!used[j] && d[j] < ss)
            {
                v = j;
                ss = d[j];
            }
        }
        set<int> st;
        int t = v;
        s += d[v];
        while(t)
        {
            st.insert(t);
            t = p[t];
        }
        used[v] = 1;
        for(j = 1; j <= n; j ++)
        {
            int h = funk(v, j) * m + d[v];
            if(!st.count(j))
                h -= a[j];
            if(!used[j] && h < d[j])
            {
                d[j] = h;
                p[j] = v;
            }
        }
    }
    if(d[n] < 0)
        d[n] = 0;
    cout << d[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,int> > q;
bool used[101];
int dis[101];
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, 1));
    while(!q.empty())
    {
        int v = q.top().second;
        used[v] = 1;
        q.pop();
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                dis[i] = min(dis[i], abs(x[i] - x[v]) + abs(y[i] - y[v]) * d - a[i]);
                q.push(make_pair(-1 * dis[i], i));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,pair<int,set<int> > > > q;
bool used[101];
int dis[101];
set<int> st;
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, make_pair(1, st)));
    while(!q.empty())
    {
        int v = q.top().second.first;
        st = q.top().second.second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                set<int> t = st;
                dis[i] = min(dis[i], abs(x[i] - x[v]) + abs(y[i] - y[v]) * d);
                if(!t.count(i))
                {
                    dis[i] -= a[i];
                    t.insert(i);
                }
                q.push(make_pair(-1 * dis[i], make_pair(i, t)));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int,pair<int,set<int> > > > q;
bool used[101];
int dis[101];
set<int> st;
int x[101], y[101], a[101];
main()
{
    int n, m, i, j, s = 0, d;
    cin >> n >> d;
    for(i = 1; i <= n; i ++)
        dis[i] = 1e15;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
        cin >> x[i] >> y[i];
    q.push(make_pair(0, make_pair(1, st)));
    dis[1] = 0;
    while(!q.empty())
    {
        int v = q.top().second.first;
        int s = q.top().first;
        st = q.top().second.second;
        used[v] = 1;
        q.pop();
        a[v] = 0;
        for(i = 1; i <= n; i ++)
        {
            if(!used[i])
            {
                set<int> t = st;
                dis[i] = min(dis[i], -1 * s + abs(x[i] - x[v]) + abs(y[i] - y[v]) * d);
                if(!t.count(i))
                {
                    dis[i] -= a[i];
                    t.insert(i);
                }
                q.push(make_pair(-1 * dis[i], make_pair(i, t)));
            }
        }
    }
    if(dis[n] < 0)
        dis[n] = 0;
    cout << dis[n];
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[1001];
int x[501], y[101];
bool used[101];
int d[101];
int p[101];
int funk(int i, int j)
{
    return abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
main()
{
    int n, m, i, j, s = 0;
    cin >> n >> m;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
    {
        cin >> x[i] >> y[i];
    }
    for(i = 1; i <= n; i ++)
        d[i] = 1e9;
    d[1] = 0;
    for(i = 1; i <= n; i ++)
    {
        int v, ss = 1e9;
        for(j = 1; j <= n; j ++)
        {
            if(!used[j] && d[j] < ss)
            {
                v = j;
                ss = d[j];
            }
        }
        set<int> st;
        int t = v;
        s += d[v];
        while(t)
        {
            st.insert(t);
            t = p[t];
        }
        used[v] = 1;
        for(j = 1; j <= n; j ++)
        {
            int h = funk(i, j) * m + d[v];
            if(!st.count(j))
                h -= a[j];
            if(!used[j] && h < d[j])
            {
                d[j] = h;
                p[j] = v;
            }
        }
    }
    if(s < 0)
        s = 0;
    cout << s;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[1001];
int x[501], y[101];
bool used[101];
int d[101];
int p[101];
int funk(int i, int j)
{
    return abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
main()
{
    int n, m, i, j, s = 0;
    cin >> n >> m;
    for(i = 2; i < n; i ++)
        cin >> a[i];
    for(i = 1; i <= n; i ++)
    {
        cin >> x[i] >> y[i];
    }
    for(i = 1; i <= n; i ++)
        d[i] = 1e9;
    d[1] = 0;
    for(i = 1; i <= n; i ++)
    {
        int v, ss = 1e9;
        for(j = 1; j <= n; j ++)
        {
            if(!used[j] && d[j] < ss)
            {
                v = j;
                ss = d[j];
            }
        }
        set<int> st;
        int t = v;
        s += d[v];
        while(t)
        {
            st.insert(t);
            t = p[t];
        }
        used[v] = 1;
        for(j = 1; j <= n; j ++)
        {
            int h = funk(i, j) * m + d[v];
            if(!st.count(j))
                h -= a[j];
            if(!used[j] && h < d[j])
            {
                d[j] = h;
                p[j] = v;
            }
        }
    }
    if(d[n] < 0)
        d[n] = 0;
    cout << d[n];
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair <int, int>
vector <PII> cor;
int dist [105][105];
int lose [105];
int key [105];
int vis [105];
int n, d;
void solve ()
{
    priority_queue <PII, vector<PII>,greater<PII> > t;
    t.push(PII(0, n));
    while (t.size())
    {
        PII x = t.top(); t.pop();
        int node = x.second, dista = x.first;
        if (vis[node]) continue;
        vis[node]=1;
        key[node]=dista;
        for (int g=1; g<=n; g++)
        {
            t.push(PII(dista+dist[node][g], g));
        }
    }
}
main()
{
    for (int g=0; g<=100; g++) key[g]=1e18;
    cin >> n >> d;
    for (int g=2; g<=n-1; g++) cin >> lose[g];
    for (int g=1; g<=n; g++)
    {
        int a, b; cin >> a >> b;
        cor.push_back(PII(a, b));
    }
    for (int g=1; g<=cor.size(); g++)
    {
        for (int y=1; y<=cor.size(); y++)
        {
            dist[g][y]=d*(abs(cor[g-1].first-cor[y-1].first)+abs(cor[g-1].second-cor[y-1].second))-lose[y];
        }
    }
    solve ();
    cout << key[1];
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair <int, int>
vector <PII> cor;
int dist [105][105];
int lose [105];
int key [105];
int vis [105];
int n, d;
void solve ()
{
    priority_queue <PII, vector<PII>,greater<PII> > t;
    t.push(PII(0, n));
    while (t.size())
    {
        PII x = t.top(); t.pop();
        int node = x.second, dista = x.first;
        if (vis[node]) continue;
        vis[node]=1;
        key[node]=dista;
        for (int g=1; g<=n; g++)
        {
            if (vis[g]) continue;
            t.push(PII(dista+dist[node][g], g));
        }
    }
}
main()
{
    for (int g=0; g<=100; g++) key[g]=1e18;
    cin >> n >> d;
    for (int g=2; g<=n-1; g++) cin >> lose[g];
    for (int g=1; g<=n; g++)
    {
        int a, b; cin >> a >> b;
        cor.push_back(PII(a, b));
    }
    for (int g=1; g<=cor.size(); g++)
    {
        for (int y=1; y<=cor.size(); y++)
        {
            dist[g][y]=d*(abs(cor[g-1].first-cor[y-1].first)+abs(cor[g-1].second-cor[y-1].second))-lose[y];
        }
    }
    solve ();
    cout << key[1];
    return 0;
}


import java.io.*;
import java.util.*;

public class Codeforces
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]),i;
        long a[]=new long[n];
        for(i=0;i<n;i++)
        {
            s=bu.readLine().split(" ");
            int ci=Integer.parseInt(s[0]),ti=Integer.parseInt(s[1]);
            ci*=ti;
            if(i!=0) a[i]=a[i-1]+ci;
            else a[i]=ci;
        }
        int j=0;
        s=bu.readLine().split(" ");
        for(i=0;i<m;i++)
        {
            long v=Long.parseLong(s[i]);
            for(;j<n;j++)
            if(a[j]>=v) break;
            sb.append(Math.min(j+1,n)+"\n");
        }
        System.out.print(sb);
    }
}


import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class TaskD {
    static int n;
    static int[] sum;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        int m = in.nextInt();

        int[] c = new int[n];
        int[] t = new int[n];
        sum = new int[n];

        int summ = 0;
        for (int i = 0; i < n; i++) {
            c[i] = in.nextInt();
            t[i] = in.nextInt();
            summ += c[i] * t[i];
            sum[i] = summ;
        }
        int q;
        int ans;
        for (int i = 0; i < m; i++) {
            q = in.nextInt();
            ans = search(q);
            System.out.println(ans);
        }
        in.close();
 //       Arrays.binarySearch(ans)
    }

    private static int search(int q) {
        int left = 0;
        int right = n-1;
        while (left < right ) {
            int mid = (right + left) >>> 1;

            if (sum[mid] < q)
                left = mid + 1;
            else if (sum[mid] > q)
                right = mid - 1;
            else
                return mid;
        }
        return left ;

    }

}


import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class TaskD {
    static int n;
    static int[] sum;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        int m = in.nextInt();
        int[] c = new int[n];
        int[] t = new int[n];
        sum = new int[n];
        int summ = 0;
        for (int i = 0; i < n; i++) {
            c[i] = in.nextInt();
            t[i] = in.nextInt();
            summ += c[i] * t[i];
            sum[i] = summ;
        }
        int q;
        for (int i = 0; i < m; i++) {
            q = in.nextInt();
            System.out.println(search(q));
        }
        in.close();
    }

    private static int search(int q) {
        int left = 0;
        int right = n-1;
        while (left < right) {
            int mid = (right + left) >>> 1;

            if (mid < q)
                left = mid + 1;
            else if (mid > q)
                right = mid - 1;
            else
                return mid;
        }
        return (left + 1);

    }

}


import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class TaskD {
    static int n;
    static int[] sum;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        int m = in.nextInt();
        int[] c = new int[n];
        int[] t = new int[n];
        sum = new int[n];
        int summ = 0;
        for (int i = 0; i < n; i++) {
            c[i] = in.nextInt();
            t[i] = in.nextInt();
            summ += c[i] * t[i];
            sum[i] = summ;
        }
        int q;
        for (int i = 0; i < m; i++) {
            q = in.nextInt();
            System.out.println(search(q));
        }
        in.close();
    }

    private static int search(int q) {
        int left = 0;
        int right = n-1;
        while (left < right) {
            int mid = (right + left) >>> 1;

            if (mid < q)
                left = mid + 1;
            else if (mid > q)
                right = mid - 1;
            else
                return mid;
        }
        return -(left + 1);

    }

}