#include <bits/stdc++.h>
bool first_comparator(const std::pair<int64_t, size_t>& left,
                      const std::pair<int64_t, size_t>& right) {
  return left.first < right.first;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int64_t m;
  size_t n;
  std::cin >> m >> n;
  std::vector<std::pair<int64_t, size_t>> data[2];
  for (auto& item : data) {
    item.resize(n);
    for (size_t j = 0; j < n; ++j) {
      std::cin >> item[j].first;
      item[j].second = j;
    }
    std::sort(item.begin(), item.end());
  }
  std::vector<int64_t> score(2 * n + 1);
  for (size_t i = 0; i < n; ++i) {
    score[0] += m - data[0][i].first + data[1][i].first;
    score[i + 1] -= m;
    score[i + n + 1] += m;
    auto a_wait = n - static_cast<size_t>(std::distance(
                          data[1].begin(),
                          std::lower_bound(data[1].begin(), data[1].end(),
                                           data[0][i], first_comparator)));
    score[i + 1 + a_wait] += 2 * data[0][i].first;
    auto b_wait = static_cast<size_t>(std::distance(
        data[0].begin(), std::upper_bound(data[0].begin(), data[0].end(),
                                          data[1][i], first_comparator)));
    score[((n - 1) - i) + 1 + b_wait] -= 2 * data[1][i].first;
  }
  int64_t min_score = score[0], curr_score = score[0];
  size_t min_i = 0;
  for (size_t i = 1; i < score.size(); ++i) {
    curr_score += score[i];
    if (curr_score < min_score) {
      min_score = curr_score;
      min_i = i;
    }
  }
  std::vector<size_t> mapping(n);
  for (size_t i = 0; i < n; ++i) {
    mapping[data[0][i].second] = data[1][(i + 3 * n - min_i) % n].second;
  }
  std::cout << min_score << "\n";
  for (size_t i = 0; i < n; ++i) {
    std::cout << (mapping[i] + 1) << " ";
  }
  return 0;
}
