#include <bits/stdc++.h>
using namespace std;
int dist(int a, int b, int L) {
  int d = abs(a - b);
  return min(d, L - d);
}
int func(int x, int y, int L) {
  if (x == y) return 0;
  if (x < y) {
    if (y - x <= L / 2) return -x;
    return L + x;
  } else {
    if (x - y <= L / 2) return x;
    return -x;
  }
}
void upd(vector<long long>& add, int x, int start, vector<int>& b, int n, int L,
         bool rev) {
  function<int(int)> foo = [&](int i) { return func(x, b[i], L); };
  int m[] = {0, x - L / 2, x, x + 1, x + L / 2 + 1};
  function<int(int)> block = [&](int y) {
    for (int t = 0; t < 4; ++t)
      if (m[t] <= y && y < m[t + 1]) return t;
    return 4;
  };
  function<int(int)> get_nxt;
  if (!rev) {
    get_nxt = [&](int y) { return m[(block(y) + 1) % 5]; };
  } else {
    get_nxt = [&](int y) { return m[block(y)]; };
  }
  int cur_pos = start, nxt_pos;
  int start_block = block(b[start]);
  for (int it = 0; it < 5; ++it) {
    nxt_pos =
        (lower_bound((b).begin(), (b).end(), get_nxt(b[cur_pos])) - b.begin()) %
        n;
    if (rev) {
      nxt_pos = (nxt_pos - 1 + n) % n;
    }
    if (!rev)
      add[(nxt_pos - start + n) % n] += foo(nxt_pos) - foo(cur_pos);
    else
      add[(start - nxt_pos + n) % n] += foo(nxt_pos) - foo(cur_pos);
    cur_pos = nxt_pos;
    if (block(b[nxt_pos]) == start_block) break;
  }
}
int main() {
  int n, L;
  scanf("%d %d", &L, &n);
  vector<pair<int, int> > a(n), b(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i].first);
    --a[i].first;
    a[i].second = i;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i].first);
    --b[i].first;
    b[i].second = i;
  }
  sort((a).begin(), (a).end());
  sort((b).begin(), (b).end());
  vector<int> aa(n), bb(n);
  for (int i = 0; i < n; ++i) aa[i] = a[i].first, bb[i] = b[i].first;
  vector<long long> add(n, 0);
  for (int i = 0; i < n; ++i) {
    upd(add, aa[i], i, bb, n, L, false);
    upd(add, bb[i], i, aa, n, L, true);
  }
  add[0] = 0;
  for (int i = 0; i < n; ++i) {
    add[0] += dist(aa[i], bb[i], L);
  }
  long long ans = (long long)1e18, sum = 0, pos_min = 0;
  for (int i = 0; i < n; ++i) {
    sum += add[i];
    if (ans > sum) {
      ans = sum;
      pos_min = i;
    }
  }
  printf("%lld\n", ans);
  vector<int> out(n, -1);
  for (int i = 0; i < n; ++i) out[a[i].second] = b[(i + pos_min) % n].second;
  for (int i = 0; i < n; ++i) printf("%d ", out[i] + 1);
  printf("\n");
}
