#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f == 1 ? x : -x;
}
inline void print(long long x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x >= 10) print(x / 10);
  putchar((x % 10) | 48);
}
long long m, n;
struct node {
  long long val, id;
  bool operator<(const node &x) const { return val < x.val; }
} a[200010], b[600010];
long long c[400010];
long long ans[200010];
void add(long long l, long long r, long long k) { c[l] += k, c[r + 1] -= k; }
long long mon(long long x) {
  while (x > n) x -= n;
  return x;
}
signed main() {
  m = read(), n = read();
  for (long long i = 1; i <= n; ++i) a[i].val = read(), a[i].id = i;
  for (long long i = 1; i <= n; ++i) b[i].val = read(), b[i].id = i;
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (long long i = 1; i <= n; ++i) a[i].val += m;
  for (long long i = 1; i <= n + n; ++i) b[i + n] = b[i], b[i + n].val += m;
  for (long long i = 1; i <= n; ++i) c[0] += a[i].val;
  for (long long i = 1; i <= n; ++i) {
    long long j = upper_bound(b + 1, b + 3 * n + 1, a[i]) - b;
    if (j > i && j <= i + 2 * n) c[j - i] -= 2 * a[i].val;
  }
  for (long long i = 1; i <= 3 * n; ++i) {
    if (i <= n) {
      add(0, i - 1, -b[i].val);
      continue;
    }
    if (i > (n << 1)) {
      add(i - n, n << 1, b[i].val);
      continue;
    }
    long long j = lower_bound(a + 1, a + n + 1, b[i]) - a;
    add(i - j + 1, i - 1, b[i].val);
    add(i - n, i - j, -b[i].val);
  }
  long long ansid = 0;
  for (long long i = 1; i <= (n << 1); ++i) {
    c[i] += c[i - 1];
    if (c[i] < c[ansid]) {
      ansid = i;
    }
  }
  printf("%lld\n", c[ansid]);
  for (long long i = 1; i <= n; ++i) ans[a[i].id] = b[mon(i + ansid)].id;
  for (long long i = 1; i <= n; ++i) printf("%d ", ans[i]);
  return 0;
}
