#include <bits/stdc++.h>
long long sum[200005], anss = 1e18;
std::pair<long long, long long> a[200005], b[200005];
long long ans[200005], n, m, val[200005], pos;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; c > '9' || c < '0'; c = getchar())
    if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
inline void update(int l, int r, int id, int val, int opt) {
  int L, R;
  if (l > r) return;
  if (opt)
    L = (id + n - r) % n, R = (id + n - l) % n;
  else
    L = (l + n - id) % n, R = (r + n - id) % n;
  if (L > R) {
    sum[L] += val;
    sum[n] -= val;
    sum[0] += val;
    sum[R + 1] -= val;
  } else
    sum[L] += val, sum[R + 1] -= val;
}
inline void work() {
  m = read(), n = read();
  for (int i = 1; i <= n; i++)
    a[i] = std::pair<long long, long long>(read(), i);
  for (int i = 1; i <= n; i++)
    b[i] = std::pair<long long, long long>(read(), i);
  std::sort(a + 1, a + n + 1);
  std::sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) val[i] = b[i].first << 1;
  for (int i = 1, p1, p2, p3; i <= n; i++) {
    p1 = std::upper_bound(val + 1, val + n + 1, 2 * a[i].first - m) - val;
    p2 = std::upper_bound(val + 1, val + n + 1, 2 * a[i].first) - val;
    p3 = std::upper_bound(val + 1, val + n + 1, 2 * a[i].first + m) - val;
    update(1, p1 - 1, i, -a[i].first, 0), update(p1, p2 - 1, i, a[i].first, 0),
        update(p2, p3 - 1, i, -a[i].first, 0),
        update(p3, n, i, a[i].first + m, 0);
  }
  for (int i = 1; i <= n; i++) val[i] = a[i].first << 1;
  for (int i = 1, p1, p2, p3; i <= n; i++) {
    p1 = std::upper_bound(val + 1, val + n + 1, 2 * b[i].first - m - 1) - val;
    p2 = std::upper_bound(val + 1, val + n + 1, 2 * b[i].first - 1) - val;
    p3 = std::upper_bound(val + 1, val + n + 1, 2 * b[i].first + m - 1) - val;
    update(1, p1 - 1, i, -b[i].first, 1), update(p1, p2 - 1, i, b[i].first, 1),
        update(p2, p3 - 1, i, -b[i].first, 1),
        update(p3, n, i, b[i].first + m, 1);
  }
  for (int i = 0; i < n; i++) {
    if (sum[i] < anss) anss = sum[i], pos = i;
    sum[i + 1] += sum[i];
  }
  printf("%I64d\n", anss);
  for (int i = 1, j = pos + 1; i <= n; i++, j = j == n ? 1 : j + 1)
    ans[a[i].second] = b[j].second;
  for (int i = 1; i <= n; i++) printf("%I64d%c", ans[i], i == n ? '\n' : ' ');
}
int main() {
  work();
  return 0;
}
