#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (T == S) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S) return EOF;
  }
  return *S++;
}
inline long long read() {
  register long long x = 0, f = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = gc();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = gc();
  }
  return (f == 1) ? x : -x;
}
long long a[1010101], b[1010101], m, n, ida[1010101], idb[1010101];
long long A[1010101], B[1010101], ans[1010101];
inline bool cmpa(long long i, long long j) { return a[i] < a[j]; }
inline bool cmpb(long long i, long long j) { return b[i] < b[j]; }
signed main() {
  m = read(), n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), ida[i] = i;
  for (int i = 1; i <= n; i++) b[i] = read(), idb[i] = i;
  sort(ida + 1, ida + 1 + n, cmpa);
  sort(idb + 1, idb + 1 + n, cmpb);
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++) {
    b[i + n] = b[i];
    b[i + 2 * n] = b[i] + m;
    b[i] -= m;
  }
  for (int i = 1, j = 0; i <= n; i++) {
    for (; b[j + 1] <= a[i] && j + 1 <= 3 * n; j++)
      ;
    if (j - i + 1 >= 1) A[1] += a[i], A[j - i + 2] -= a[i];
    if (j - i + 2 <= 2 * n + 1)
      A[max(1, j - i + 2)] -= a[i], A[2 * n + 2] += a[i];
  }
  for (int j = 1, i = 0; j <= 3 * n; j++) {
    for (; b[j] > a[i + 1] && i + 1 <= n; i++)
      ;
    if (j - i + 1 <= j) B[max(1, j - i + 1)] += b[j], B[j + 1] -= b[j];
    if (max(1ll, j - n + 1) <= j - i + 1 && j - i + 1 > 0)
      B[max(1ll, j - n + 1)] -= b[j], B[j - i + 1] += b[j];
  }
  for (int i = 1; i <= 2 * n + 1; i++) {
    A[i] += A[i - 1];
    B[i] += B[i - 1];
  }
  long long ansid = 0;
  A[ansid] = B[ansid] = 99999999999999;
  for (int i = 1; i <= 2 * n + 1; i++) {
    if (A[ansid] + B[ansid] > A[i] + B[i]) ansid = i;
  }
  cout << A[ansid] + B[ansid] << "\n";
  for (int i = 1; i <= n; i++) {
    ans[ida[i]] = idb[(ansid + i - 2) % n + 1];
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld ", ans[i]);
  }
  return 0;
}
