#include <bits/stdc++.h>
const int maxn = 4e5 + 3;
int n, m, a[maxn], b[maxn], ao[maxn], bo[maxn];
int match[maxn];
long long calc(int o) {
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    ans +=
        abs(a[ao[i]] - (b[bo[(i + o) % n]] + ((i + o) / n - 1) * (long long)m));
  }
  return ans;
}
int main() {
  srand(time(nullptr));
  scanf("%d%d", &m, &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    ao[i] = i;
  }
  std::sort(ao, ao + n, [](int i, int j) { return a[i] < a[j]; });
  for (int i = 0; i < n; ++i) {
    scanf("%d", b + i);
    bo[i] = i;
  }
  std::sort(bo, bo + n, [](int i, int j) { return b[i] < b[j]; });
  int l = 0, r = 3 * n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (calc(mid + 1) - calc(mid) >= 0) {
      r = mid;
    } else {
      l = mid;
    }
  }
  int ansi = r;
  long long ans = calc(ansi);
  for (int i = std::max(0, r - 2); i <= r + 2; ++i) {
    auto w = calc(i);
    if (w < ans) {
      ans = w;
      ansi = i;
    }
  }
  printf("%lld\n", ans);
  for (int i = 0; i < n; ++i) {
    match[ao[i]] = bo[(i + ansi) % n] + 1;
  }
  for (int i = 0; i < n; ++i) {
    printf("%d%c", match[i], " \n"[i == n - 1]);
  }
  return 0;
}
