#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
struct node {
  long long a;
  int id;
  friend bool operator<(node p, node q) {
    if (p.a == q.a) return p.id < q.id;
    return p.a < q.a;
  }
} a[N], b[3 * N];
int m, n, p, c[N];
long long ans = (1ll << 60), sum, f[3 * N];
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].a);
    a[i].id = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i].a);
    b[i].id = i;
    b[i + n] = b[i];
    b[i + n].a += m;
    b[i + (n << 1)] = b[i];
    b[i + (n << 1)].a -= m;
  }
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n * 3);
  for (int i = 1, j = 1; i <= n; i++) {
    while (b[j].a <= a[i].a) j++;
    f[0] += a[i].a;
    if (j - i + 1 <= 2 * n + 1) f[j - i + 1] -= (a[i].a << 1);
  }
  for (int j = 1, i = 1; j <= n * 3; j++) {
    f[max(0, j - n + 1)] -= b[j].a;
    while (i <= n && a[i].a < b[j].a) i++;
    if (j - i + 2 <= n * 2 + 1) f[j - i + 2] += (b[j].a << 1);
    f[min(n * 2 + 2, j + 1)] -= b[j].a;
  }
  sum = f[0];
  for (int i = 1; i <= 2 * n + 1; i++) {
    sum += f[i];
    if (sum < ans) {
      p = i;
      ans = sum;
    }
  }
  for (int i = 1; i <= n; i++) c[a[i].id] = b[p + i - 1].id;
  printf("%lld\n", ans);
  for (int i = 1; i <= n; i++) printf("%d ", c[i]);
  return 0;
}
