#include <bits/stdc++.h>
bool debug = false;
using namespace std;
template <class T1>
void deb(T1 e1) {
  if (debug) {
    cout << "[DEBUG]";
    cout << e1 << endl;
  }
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  if (debug) {
    cout << "[DEBUG]";
    cout << e1 << " " << e2 << endl;
  }
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  if (debug) {
    cout << "[DEBUG]";
    cout << e1 << " " << e2 << " " << e3 << endl;
  }
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  if (debug) {
    cout << "[DEBUG]";
    cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
  }
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  if (debug) {
    cout << "[DEBUG]";
    cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
  }
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  if (debug) {
    cout << "[DEBUG]";
    cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
         << endl;
  }
}
template <typename T>
void print_vector_debug(const T& t) {
  if (debug) {
    cout << "[DEBUG] VECTOR:";
    for (auto i = t.cbegin(); i != t.cend(); ++i) {
      if ((i + 1) != t.cend()) {
        cout << *i << " ";
      } else {
        cout << *i << endl;
      }
    }
  }
}
template <typename T>
void print_array_debug(const T arr, int size) {
  if (debug) {
    cout << "[DEBUG] VECTOR:";
    for (int i = 0; i < size; ++i) {
      cout << arr[i] << " ";
    }
    cout << endl;
  }
}
template <typename T>
void print_2Darray_debug(const T arr, int rows, int cols) {
  if (debug) {
    cout << "[DEBUG] Matrix:" << endl;
    for (int i = 0; i < rows; ++i) {
      for (int j = 0; j < cols; ++j) {
        cout << arr[i][j] << " ";
      }
      cout << endl;
    }
    cout << endl;
  }
}
template <typename T>
void print_matrix_debug(const T& t) {
  if (debug) {
    cout << "[DEBUG] MATRIX:" << endl;
    for (auto i = t.cbegin(); i != t.cend(); ++i) {
      for (auto j = i->cbegin(); j != i->cend(); ++j) {
        cout << *j << " ";
      }
      cout << endl;
    }
  }
}
long long sz;
long long m;
long long min_move(long long source, long long target) {
  long long move;
  if (source > target) {
    move = (m - 1) - source + target + 1;
  } else {
    move = target - source;
  }
  return move;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> sz >> m;
  vector<int> values(sz);
  vector<long long> original_values(sz);
  vector<int> freq(m, 0);
  for (int i = 0; i < sz; ++i) {
    cin >> values[i];
    original_values[i] = values[i];
    values[i] %= m;
    freq[values[i]]++;
  }
  int not_completed = 0;
  long long target = sz / m;
  long long moves = 0;
  set<int> not_completed_set;
  for (int i = 0; i < m; ++i) {
    if (freq[i] < target) {
      not_completed_set.insert(i);
    }
  }
  for (int i = 0; i < sz; ++i) {
    if (freq[values[i]] <= target) {
      continue;
    }
    auto it = not_completed_set.lower_bound(values[i]);
    if (it == not_completed_set.end()) {
      not_completed = *not_completed_set.begin();
    } else {
      not_completed = *it;
    }
    long long min_moves = min_move(values[i], not_completed);
    moves += min_moves;
    freq[values[i]]--;
    freq[not_completed]++;
    if (freq[not_completed] == target) {
      not_completed_set.erase(not_completed);
    }
    original_values[i] += min_moves;
  }
  printf("%lld\n", moves);
  for (auto& e : original_values) {
    printf("%lld ", e);
  }
  return 0;
}
