#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9, mod = 998244353;
long long res, ans, sum1[1000010], sum2[1000010], *s1 = sum1 + 1000010 / 2,
                                                  *s2 = sum2 + 1000010 / 2;
int n, m, n1, n2, sl, fh, p[1000010], mc[1000010];
struct node {
  int p, id, ty;
  bool operator<(const node &x) const { return p < x.p; }
} a[1000010];
queue<int> q;
int rd() {
  sl = 0;
  fh = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
  return sl * fh;
}
int main() {
  m = rd();
  n = rd();
  for (int i = 1; i <= n; ++i) a[i].p = rd(), a[i].ty = 1, a[i].id = i;
  for (int i = n + 1; i <= n * 2; ++i) a[i].p = rd(), a[i].ty = 2, a[i].id = i;
  sort(a + 1, a + n * 2 + 1);
  for (int i = 1; i <= n * 2; ++i)
    if (a[i].ty == 1) {
      p[i] = n1 - n2;
      s1[p[i]] += a[i].p;
      n1++;
    } else {
      p[i] = n2 - n1;
      s2[p[i]] += a[i].p;
      n2++;
    }
  for (int i = 0; i <= n; ++i) res -= s1[i] + s2[i];
  for (int i = -n; i < 0; ++i) res += s1[i] + s2[i];
  ans = res;
  int ps = 0;
  for (int i = 1; i < n * 2; ++i) {
    if (a[i].ty == 1) {
      res += s1[p[i]];
      s1[p[i]] += m;
      res += s1[p[i]];
      res -= s2[-p[i] - 1] * 2;
    } else {
      res += s2[p[i]];
      s2[p[i]] += m;
      res += s2[p[i]];
      res -= s1[-p[i] - 1] * 2;
    }
    if (res < ans) ps = i, ans = res;
  }
  printf("%lld\n", ans);
  for (int i, j, ii = 1; ii <= n * 2; ++ii) {
    i = (ps + ii - 1) % (n * 2) + 1;
    if (a[i].ty == 1) {
      if (!q.empty() && a[q.front()].ty == 2) {
        j = q.front();
        q.pop();
        mc[a[i].id] = a[j].id;
        mc[a[j].id] = a[i].id;
      } else
        q.push(i);
    } else {
      if (!q.empty() && a[q.front()].ty == 1) {
        j = q.front();
        q.pop();
        mc[a[i].id] = a[j].id;
        mc[a[j].id] = a[i].id;
      } else
        q.push(i);
    }
  }
  for (int i = 1; i <= n; ++i) printf("%d ", mc[i] - n);
  puts("");
  return 0;
}
