#include <bits/stdc++.h>
using namespace std;
const int maxn = 500500;
int tot;
struct node {
  long long x;
  int id, op;
} p[maxn];
int n, l, op[maxn * 4], d[maxn * 4], out[maxn * 4];
long long a[maxn * 4], b[maxn * 4];
long long read() {
  long long x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
bool cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.op < b.op); }
int main() {
  {
    l = read();
    n = read();
    for (int i = 1; i <= n; i++) {
      a[i] = read() - 1;
      p[++tot] = {a[i], i, 0};
    }
    for (int i = 1; i <= n; i++) {
      b[i] = read() - 1;
      p[++tot] = {b[i], i, 1};
    }
    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + n);
    sort(p + 1, p + 1 + tot, cmp);
    int i = 1, j = 1, tot = 0, now = n;
    while (i <= n || j <= n) {
      if (j > n || (i <= n && a[i] <= b[j]))
        op[++tot] = 0, d[tot] = a[i++];
      else
        op[++tot] = 1, d[tot] = b[j++];
    }
    for (int i = 0; i <= 4 * n; i++) a[i] = b[i] = 0;
    long long sum = 0, ans;
    for (int i = 1; i <= 2 * n; i++) {
      if (!op[i]) {
        a[++now] += d[i];
        if (now > n)
          sum -= d[i];
        else
          sum += d[i];
      } else {
        b[--now] += d[i];
        if (now < n)
          sum -= d[i];
        else
          sum += d[i];
      }
    }
    ans = sum;
    int imp = 0;
    for (int i = 1; i <= 2 * n; i++) {
      if (!op[i]) {
        sum -= b[now] * 2;
        now++;
        sum += a[now] + a[now] + l;
        a[now] += l;
      } else {
        sum -= a[now] * 2;
        now--;
        sum += b[now] + b[now] + l;
        b[now] += l;
      }
      if (sum < ans) ans = sum, imp = i;
    }
    vector<int> vec1, vec2;
    vec1.clear();
    vec2.clear();
    for (int i = imp + 1; i <= 2 * n; i++) {
      if (p[i].op == 0)
        vec1.push_back(p[i].id);
      else
        vec2.push_back(p[i].id);
    }
    for (int i = 1; i <= imp; i++) {
      if (p[i].op == 0)
        vec1.push_back(p[i].id);
      else
        vec2.push_back(p[i].id);
    }
    for (int i = 0; i < n; i++) out[vec1[i]] = vec2[i];
    printf("%lld\n", ans);
    for (int i = 1; i <= n; i++) printf("%d ", out[i]);
  }
  return 0;
}
