#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& cerr, vector<long long> aux) {
  cerr << "[";
  for (auto e : aux) cerr << e << ' ';
  cerr << "]";
  return cerr;
}
const int maxN = 200011;
struct analyzer {
  long long sum = 0;
  int base = maxN;
  long long up[maxN * 2], down[maxN * 2];
  void init() {
    sum = 0;
    base = maxN;
    memset(up, 0, sizeof(up));
    memset(down, 0, sizeof(down));
  }
  void insert_up(long long value) {
    sum -= 2LL * up[base];
    sum += 2LL * down[base - 1];
    base--;
    up[base + 1] += value;
    sum -= value;
  }
  void insert_down(long long value) {
    sum -= 2LL * down[base];
    sum += 2LL * up[base + 1];
    base++;
    down[base - 1] += value;
    sum -= value;
  }
  void remove_up(long long value) {
    up[base] -= value;
    sum -= value;
  }
  void remove_down(long long value) {
    down[base] -= value;
    sum -= value;
  }
};
long long m, x;
int n;
long long a[maxN], b[maxN];
vector<pair<long long, int> > order;
int answer[maxN];
analyzer graph;
void remove_duplicates(vector<pair<long long, int> >& order) {
  auto aux = order;
  vector<pair<long long, int> > plus, minus;
  order.clear();
  int j;
  for (int i = 0; i < aux.size(); i = j) {
    plus.clear();
    minus.clear();
    for (j = i; j < aux.size() && aux[j].first == aux[i].first; j++) {
      if (aux[j].second < 0)
        minus.push_back(aux[j]);
      else
        plus.push_back(aux[j]);
    }
    while (!minus.empty() && !plus.empty()) {
      answer[plus.back().second] = -minus.back().second;
      minus.pop_back();
      plus.pop_back();
    }
    while (!minus.empty()) {
      order.push_back(minus.back());
      minus.pop_back();
    }
    while (!plus.empty()) {
      order.push_back(plus.back());
      plus.pop_back();
    }
  }
}
void find_best() {
  int cnt = order.size();
  pair<long long, int> best = make_pair(1LL << 60, -1);
  for (int i = 0; i < cnt; i++)
    order.push_back(make_pair(order[i].first + m, order[i].second));
  for (int i = 2 * cnt - 1; i >= 0; i--) {
    if (i < cnt) {
      if (order[i + cnt].second < 0)
        graph.remove_down(order[i + cnt].first);
      else
        graph.remove_up(order[i + cnt].first);
    }
    if (order[i].second < 0)
      graph.insert_down(order[i].first);
    else
      graph.insert_up(order[i].first);
    if (i < cnt) best = min(best, make_pair(graph.sum, i));
  }
  vector<int> minus, plus;
  minus.clear();
  plus.clear();
  for (int i = 0; i < cnt; i++) {
    if (order[best.second + i].second < 0)
      minus.push_back(-order[best.second + i].second);
    else
      plus.push_back(order[best.second + i].second);
  }
  for (int i = 0; 2 * i < cnt; i++) {
    answer[plus[i]] = minus[i];
  }
  if (cnt == 0) best.first = 0;
  printf("%lld\n", best.first);
  for (int i = 1; i <= n; i++) printf("%d ", answer[i]);
}
int main() {
  scanf("%lld%d", &m, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    order.push_back(make_pair(a[i], i));
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
    order.push_back(make_pair(b[i], -i));
  }
  sort(order.begin(), order.end());
  remove_duplicates(order);
  find_best();
  return 0;
}
