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

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

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


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

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


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


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

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

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


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

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


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

        long long ans = 0;
        int last = m-1;
        vector< pair< int , int > >need;
        for( int i = 0 ; i < 2*m ; i++ )
        {
                int idx = i % m;
                while( v[ idx ].size() > req )
                {
                        int val = v[ idx ].back();
                        v[ idx ].pop_back();
                        need.push_back( { val , i } );
                }
                while( v[ idx ].size() < req && need.size() )
                {
                        int val = need.back().first;
                        int add = need.back().second;
                        need.pop_back();
                        ar[ val ] +=  i - add;
                        ans += i - add;
                        v[ idx ].push_back( val );
                }
        }
        printf("%lld\n",ans);
        for( int i = 0 ; i < n ; i++ )
        {
                printf("%lld%s" , ar[ i ] , i < n-1 ? " " : "\n");
        }
}
int main()
{
        //freopen("input.txt","r",stdin );
        //freopen("output.txt","w",stdout );
        solve();
        return 0;
}


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

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

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


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

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

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


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

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


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

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


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

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

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


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				auto temp = free.end();
				count += (i - arr[*temp] % m);
				arr[*temp] += (i - arr[*temp] % m);
				arr2[i].push_back(*temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	// for (int i = 0; i < m; ++i)
	// {
	// 	for (int j = 0; j < arr2[i].size(); ++j)
	// 	{
	// 		cout << arr2[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		// for (int j = 0; j < free.size(); ++j)
		// {
		// 	cout << free[j] << " ";
		// }
		// cout << endl;

	}

	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				// auto temp = free.end();
				// count += (i - arr[*temp] % m);
				// arr[*temp] += (i - arr[*temp] % m);
				// arr2[i].push_back(*temp);
				// free.pop_back();
				if (free.size() == 0)
				{
					cout << -1 << endl;
					return 0;
				}

				ll temp = free[free.size() - 1];
				//cout << "temp :" << temp << endl;
				count += (-i + arr[temp]) % m;
				arr[temp] += (-i + arr[temp]) % m;
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

void erasePos(vector<int> &arr, int pos) {
	int j = 0;
	for (auto it = arr.begin(); it != arr.end(); ++it) {
		if (j == pos)
		{
			arr.erase(it);
		}
	}
}

int main() {
	_init();

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

	vector<int> free;
	int count = 0;
	for (int i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<int>::iterator it = arr2[i].begin(), it1;
			int temp = 0;
			while (temp <= 0) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (int j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				int temp = free[free.size() - 1];
				count += (i - arr[temp] % m);
				arr[temp] += (i - arr[temp] % m);
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

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


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				ll temp = free[free.size() - 1];
				count += (i - arr[temp] % m);
				arr[temp] += (i - arr[temp] % m);
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp <= k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				ll temp = free[free.size() - 1];
				count += (i - arr[temp] % m);
				arr[temp] += (i - arr[temp] % m);
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	// for (int i = 0; i < m; ++i)
	// {
	// 	for (int j = 0; j < arr2[i].size(); ++j)
	// 	{
	// 		cout << arr2[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		// for (int j = 0; j < free.size(); ++j)
		// {
		// 	cout << free[j] << " ";
		// }
		// cout << endl;

		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				// auto temp = free.end();
				// count += (i - arr[*temp] % m);
				// arr[*temp] += (i - arr[*temp] % m);
				// arr2[i].push_back(*temp);
				// free.pop_back();
				if (free.size() == 0)
				{
					cout << -1 << endl;
					return 0;
				}

				ll temp = free[free.size() - 1];
				//cout << "temp :" << temp << endl;
				count += (i - arr[temp] % m);
				arr[temp] += (i - arr[temp] % m);
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	list<ll> free;
	ll count = 0;
	// for (int i = 0; i < m; ++i)
	// {
	// 	for (int j = 0; j < arr2[i].size(); ++j)
	// 	{
	// 		cout << arr2[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		// for (int j = 0; j < free.size(); ++j)
		// {
		// 	cout << free[j] << " ";
		// }
		// cout << endl;

	}

	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				// auto temp = free.end();
				// count += (i - arr[*temp] % m);
				// arr[*temp] += (i - arr[*temp] % m);
				// arr2[i].push_back(*temp);
				// free.pop_back();
				if (free.size() == 0)
				{
					cout << -1 << endl;
					return 0;
				}

				ll temp = free.front();
				if ((i - arr[temp]) >= 0)
				{
					//cout << "temp :" << temp << endl;
					count += (i - arr[temp] % m + m) % m;
					arr[temp] += (i - arr[temp] % m + m) % m;
					arr2[i].push_back(temp);
					free.pop_front();
				} else {
					//cout << "temp :" << temp << endl;
					temp = free.back();
					count += (i - arr[temp] % m + m) % m;
					arr[temp] += (i - arr[temp] % m + m) % m;
					arr2[i].push_back(temp);
					free.pop_back();
				}

			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	list<ll> free;
	ll count = 0;
	// for (int i = 0; i < m; ++i)
	// {
	// 	for (int j = 0; j < arr2[i].size(); ++j)
	// 	{
	// 		cout << arr2[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		// for (int j = 0; j < free.size(); ++j)
		// {
		// 	cout << free[j] << " ";
		// }
		// cout << endl;

	}

	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				// auto temp = free.end();
				// count += (i - arr[*temp] % m);
				// arr[*temp] += (i - arr[*temp] % m);
				// arr2[i].push_back(*temp);
				// free.pop_back();
				if (free.size() == 0)
				{
					cout << -1 << endl;
					return 0;
				}

				ll temp = free.front();
				//cout << "temp :" << temp << endl;
				count += (i - arr[temp] % m + m) % m;
				arr[temp] += (i - arr[temp] % m + m) % m;
				arr2[i].push_back(temp);
				free.pop_front();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

void erasePos(vector<ll> &arr, ll pos) {
	ll j = 0;
	for (auto it = arr.begin(); it != arr.end(); ++it) {
		if (j == pos)
		{
			arr.erase(it);
		}
	}
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp <= 0) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				ll temp = free[free.size() - 1];
				count += (i - arr[temp] % m);
				arr[temp] += (i - arr[temp] % m);
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	vector<ll> free;
	ll count = 0;
	// for (int i = 0; i < m; ++i)
	// {
	// 	for (int j = 0; j < arr2[i].size(); ++j)
	// 	{
	// 		cout << arr2[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		// for (int j = 0; j < free.size(); ++j)
		// {
		// 	cout << free[j] << " ";
		// }
		// cout << endl;

	}

	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				// auto temp = free.end();
				// count += (i - arr[*temp] % m);
				// arr[*temp] += (i - arr[*temp] % m);
				// arr2[i].push_back(*temp);
				// free.pop_back();
				if (free.size() == 0)
				{
					cout << -1 << endl;
					return 0;
				}

				ll temp = free[free.size() - 1];
				//cout << "temp :" << temp << endl;
				count += (i - arr[temp] % m + m) % m;
				arr[temp] += (i - arr[temp] % m + m) % m;
				arr2[i].push_back(temp);
				free.pop_back();
			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long int
#define maxn 200005

void _init() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	_init();

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

	list<ll> free;
	ll count = 0;
	// for (int i = 0; i < m; ++i)
	// {
	// 	for (int j = 0; j < arr2[i].size(); ++j)
	// 	{
	// 		cout << arr2[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }
	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() > k)
		{
			vector<ll>::iterator it = arr2[i].begin(), it1;
			ll temp = 0;
			while (temp < k) {
				++it;
				temp++;
			}
			it1 = it;
			for (; it != arr2[i].end(); ++it) {
				free.push_back(*it);
			}
			arr2[i].erase(it1, arr2[i].end());
			// for (ll j = 0; j < arr2[i].size() - k; ++j)
			// {
			// 	free.push_back(arr2[i][j]);
			// 	erasePos(arr2[i], j);
			// }
		}

		// for (int j = 0; j < free.size(); ++j)
		// {
		// 	cout << free[j] << " ";
		// }
		// cout << endl;

	}

	for (ll i = 0; i < m; ++i)
	{
		if (arr2[i].size() < k)
		{
			while (arr2[i].size() != k) {
				// auto temp = free.end();
				// count += (i - arr[*temp] % m);
				// arr[*temp] += (i - arr[*temp] % m);
				// arr2[i].push_back(*temp);
				// free.pop_back();
				if (free.size() == 0)
				{
					cout << -1 << endl;
					return 0;
				}

				ll temp = free.front();
				ll temp2 = free.back();
				ll count1 = (i - arr[temp] % m + m) % m;
				ll count2 = (i - arr[temp2] % m + m) % m;
				if (count1 <= count2)
				{
					//cout << "temp :" << temp << endl;
					count += count1;
					arr[temp] += count1;
					arr2[i].push_back(temp);
					free.pop_front();
				} else {
					//cout << "temp :" << temp << endl;
					count += count2;
					arr[temp2] += count2;
					arr2[i].push_back(temp2);
					free.pop_back();
				}

			}
		}
	}

	cout << count << endl;
	for (ll i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // cout<<arr[i].size()<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;p>=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll li=t-arr[i].size();
            ll s=free.size();
            for(ll j=free.size()-1, c=1; c<=li && j>=0; c++, j--)
            {
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // cout<<arr[i].size()<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;p>=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll s=free.size();
            ll j=free.size()-1;
            for(; j>=0 && arr[i].size()<t; j--)
            {
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    Codeforces Contest 992 (Div 2)....
    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n, 0);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> c(m, 0);
    for(ll i=0;i<n;i++)
    {
        c[v[i]%m]++;
    }
    set<ll> s;
    vector<pair<ll,ll> > l;
    for(ll i=0;i<m;i++)
    {
        if(c[i]<=n/m) s.insert(i), s.insert(i+m);
        else l.pb({c[i], i});
    }
    sort(all(l));
    ll ans=0;
    vector<pair<ll,ll> > ch[m+5];
    for(ll i=l.size()-1;i>=0;i--)
    {
        ll idx=l[i].se;
        if(c[idx]==n/m) continue;
        auto it=lower_bound(s.begin(), s.end(), idx);
        ll j=*it;
        ll g=min(n/m-c[j%m], c[idx]-n/m);
        c[j%m]+=g;
        c[idx]-=g;
        ans+=g*(j-idx);
        if(c[j%m]==n/m)
        {
            if(s.find(j%m)!=s.end()) s.erase(s.find(j%m));
            if(s.find(j%m+m)!=s.end()) s.erase(s.find(j%m+m));
        }
        ch[idx].pb({j%m, g});
        if(c[idx]==n/m) continue;
        else i++;
    }
    cout<<ans<<endl;
    vector<ll> co(m, n/m);
    for(ll i=0;i<v.size();i++)
    {
        ll idx=v[i]%m;
        if(co[idx]>0) cout<<v[i]<<" ", co[idx]--;
        else
        {
            ll j=ch[idx][ch[idx].size()-1].fs;
            cout<<v[i] + abs(j-idx)<<" ";
            ch[idx][ch[idx].size()-1].se--;
            if(ch[idx][ch[idx].size()-1].se==0) ch[idx].pop_back();
        }
    }

  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // if(n==200000 && m==2)    cout<<i<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;arr[i].size()!=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll x=free.size();
            ll j=x-1;
            // if(m==2 && n==200000) cout<<j<<endl<<free.size()<<endl;
            for(; j>=0 && arr[i].size()<t; j--)
            {
                // if(m==2 && n==200000) cout<<j<<endl;
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // if(n==200000 && m==2)    cout<<i<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;arr[i].size()!=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            if(free.size()==0) continue;
            ll j=free.size()-1;
            for(; j>=0 && arr[i].size()<t; j--)
            {
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // cout<<arr[i].size()<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-t;p>=t-1;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll li=t-arr[i].size();
            ll s=free.size();
            for(ll j=free.size()-1, c=1; c<=li && j>=0; c++, j--)
            {
                arr[i].pb(free[j]);
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // if(n==200000 && m==2)    cout<<i<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;arr[i].size()!=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll x=free.size();
            j=x-1;
            // if(m==2 && n==200000) cout<<j<<endl<<free.size()<<endl;
            for(; j>=0 && arr[i].size()<t; j--)
            {
                // if(m==2 && n==200000) cout<<j<<endl;
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    Codeforces Contest 992 (Div 2)....
    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
// #ifndef LOCAL
// #pragma GCC optimize("O3")
// #endif
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
// #pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n, 0);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> c(m, 0);
    for(ll i=0;i<n;i++)
    {
        c[v[i]%m]++;
    }
    set<ll> s;
    vector<pair<ll,ll> > l;
    for(ll i=0;i<m;i++)
    {
        if(c[i]<n/m) s.insert(i), s.insert(i+m);
        else if(c[i]>n/m) l.pb({c[i], i});
    }
    sort(all(l));
    ll ans=0;
    vector<pair<ll,ll> > ch[m+5];
    for(ll i=l.size()-1;i>=0;i--)
    {
        ll idx=l[i].se;
        if(c[idx]==n/m) continue;
        auto it=lower_bound(s.begin(), s.end(), idx);
        ll j=*it;
        ll g=min(n/m-c[j%m], c[idx]-n/m);
        c[j%m]+=g;
        c[idx]-=g;
        ans+=g*(j-idx);
        if(c[j%m]==n/m)
        {
            if(s.find(j%m)!=s.end()) s.erase(s.find(j%m));
            if(s.find(j%m+m)!=s.end()) s.erase(s.find(j%m+m));
        }
        ch[idx].pb({j%m, g});
        if(c[idx]==n/m) continue;
        else i++;
    }
    cout<<ans<<endl;
    vector<ll> co(m, n/m);
    for(ll i=0;i<v.size();i++)
    {
        ll idx=v[i]%m;
        if(co[idx]>0) cout<<v[i]<<" ", co[idx]--;
        else
        {
            ll j=ch[idx][ch[idx].size()-1].fs;
            cout<<v[i] + abs(j-idx)<<" ";
            ch[idx][ch[idx].size()-1].se--;
            if(ch[idx][ch[idx].size()-1].se==0) ch[idx].pop_back();
        }
    }

  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        if(n==200000 && m==2)    cout<<i<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;p>=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll j=free.size()-1;
            for(; j>=0 && arr[i].size()<t; j--)
            {
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}


/*  ---------------  Copyrighted With  -----------------

   -----   ----   -------   |      |   ------   ------     -----    -----  |     /
  |         |     |     |   |      |  |         |     |   |        |       |   /
  |         |     |------   |------|   -----    |-----     -----   |         /\
  |         |     |         |      |  |         |  \      |        |       |   \
   -----   ----   |         |      |   ------   |    \     ------   ----   |     \

    High Ratings for everyone......

*/

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

/* ----------------------------- START of Template ---------------*/
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop") // to pop argument of function as soon as it returns
#define ll long long
#define pb push_back
#define fs first
#define se second
#define rd(x) scanf("%lld", &x)
#define all(v) v.begin(),v.end()
#define pr(i, j) pair< i , j >
#define mp(i, j) map< i , j >
#define rep(i, a, b) for(ll i=a; i<b; ++i)
#define repe(i, a, b) for(ll i=a; i<=b; ++i)
#define jrep(i, a, b, c) for(ll i=a; i<b; i+=c)
#define jrepe(i, a, b, c) for(ll i=a;i<=b; i+=c)
#define inp(i, n, a) for(ll i=0;i<n;i++) cin>>a[i];
ll dx[4]={-1, 0, 0, +1};
ll dy[4]={0, -1, +1, 0};
const ll LINF = 1e18;
const ll INF = 1e9;
const ll MOD = 1e9+7;

ll strtoll(string s) { stringstream str(s); ll number=0; str>>number; return number; }

string lltostr(ll n){ stringstream ss; ss<<n; return ss.str(); }

ll llsqrt(ll x){ if(x==0 || x==1) return x; ll start=1, end=x/2, ans; while(start<=end) { ll mid=(start+end)/2; if(mid*mid==x) return mid;
        else if(mid <= x/mid) ans=mid,start=mid+1; else end=mid-1; }  return ans; }

vector<ll> spf;
void sieve(ll size) { spf.resize(size+1); for(ll i=1;i<=size; ++i) spf[i]=i; for(ll i=4;i<=size; i+=2) spf[i]=2;
    for(ll i=3;i*i<=size; ++i) { if(spf[i]==i) { for(ll j=i*i; j<=size; j+=i) if(spf[j]==j) spf[j]=i; } } }

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m); t=(t*t)%m; if(b&1)t=(t*a)%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

void fastscan(ll &number){ bool negative = false; register ll c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); }
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative)  number *= -1;   }

/* ----------------------          End Of Template --------------------------------*/

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
   // clock_t tStart = clock();

  /*---------------------------------------------------------------------------------------------------*/

    ll n, m;
    cin>>n>>m;
    vector<ll> v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll> free;
    ll ans=0;
    vector<ll> arr[m];
    for(ll i=0;i<n;i++)
    {
        arr[v[i]%m].pb(i);
    }
    ll t=n/m;
    for(ll idx=0;idx<2*m;idx++)
    {
        ll i=idx%m;
        // if(n==200000 && m==2)    cout<<i<<endl;
        if(arr[i].size()>t)
        {
            for(ll p=arr[i].size()-1;arr[i].size()!=t;p--)
            {
                free.pb(arr[i][p]);
                arr[i].pop_back();
            }
        }
        else
        {
            ll j=free.size()-1;
            for(; j>=0 && arr[i].size()<t; j--)
            {
                if(n==200000 && m==2) cout<<j<<endl;
                arr[i].pb(free[j]);
                // cout<<free[j]<<" ";
                ans+=((i-v[free[j]]%m)%m + m)%m;
                free.pop_back();
            }
        }
        // cout<<arr[i].size()<<endl;
    }
    cout<<ans<<endl;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<t;j++)
        {
            ll idx=arr[i][j];
            if(v[idx]%m>i)
            {
                v[idx]+=(m-(v[idx]%m-i));
            }
            else
            {
                v[idx]+=(i-v[idx]%m);
            }
        }
    }
    for(ll i=0;i<n;i++) cout<<v[i]<<" ";
  /*---------------------------------------------------------------------------------------------------*/

   // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}