#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') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 2e5 + 10;
int n, m, Ans[N];
pair<int, int> a[N], b[N];
inline long long Query(int x) {
  long long ret = 0;
  if (x < 0) {
    x = -x;
    for (register int i = (1); i <= (x); i++)
      ret += m - a[n - x + i].first + b[i].first,
          Ans[a[n - i + 1].second] = b[i].second;
    for (register int i = (x + 1); i <= (n); i++)
      ret += abs(b[i].first - a[i - x].first),
          Ans[a[i - x].second] = b[i].second;
  } else {
    for (register int i = (1); i <= (x); i++)
      ret += a[i].first + m - b[n - x + i].first,
          Ans[a[i].second] = b[n - x + i].second;
    for (register int i = (x + 1); i <= (n); i++)
      ret += abs(a[i].first - b[i - x].first),
          Ans[a[i].second] = b[i - x].second;
  }
  return ret;
}
int main() {
  m = read(), n = read();
  for (register int i = (1); i <= (n); i++) a[i] = make_pair(read(), i);
  for (register int i = (1); i <= (n); i++) b[i] = make_pair(read(), i);
  sort(a + 1, a + 1 + n), sort(b + 1, b + 1 + n);
  int l = -n, r = n;
  while (l < r) {
    int mid = l + r >> 1;
    if (Query(mid) < Query(mid + 1))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", Query(l));
  for (register int i = (1); i <= (n); i++) printf("%d ", Ans[i]);
}
