#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const long long linf = 2e18;
const long long mod = 998244353;
const double eps = 1e-7;
template <class T>
void read(T &a) {
  int f = 1;
  a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  a *= f;
}
long long n, m, minn = linf, cnt;
long long ta[maxn], tb[maxn];
pair<long long, long long> a[maxn], b[maxn], ans[maxn];
long long sum[maxn];
void Sub(long long cnt, long long now, long long l, long long r, long long s) {
  if (l > r) return;
  if (cnt == 1) {
    l -= now - 1;
    r -= now - 1;
  } else {
    l = now - l + 1;
    r = now - r + 1;
    swap(l, r);
  }
  if (l < 1 && r < 1) l += n, r += n;
  if (l > 0) sum[l] += s, sum[r + 1] -= s;
  if (l < 1) {
    sum[1] += s, sum[r + 1] -= s;
    l += n;
    sum[l] += s, sum[n + 1] -= s;
  }
  return;
}
void work(long long *a, long long *b, long long cnt) {
  for (long long i = (1); i <= (n); ++i) {
    long long pos1 = lower_bound(b + 1, b + n + 1, a[i]) - b;
    long long pos11 = upper_bound(b + 1, b + n + 1, a[i]) - b;
    long long pos2 = upper_bound(b + 1, b + n + 1, a[i] + m / 2) - b;
    long long pos3 = lower_bound(b + 1, b + n + 1, a[i] - m / 2) - b;
    Sub(cnt, i, 1, pos3 - 1, m - a[i]);
    Sub(cnt, i, pos3, pos1 - 1, a[i]);
    Sub(cnt, i, pos11, pos2 - 1, -a[i]);
    Sub(cnt, i, pos2, n, a[i]);
  }
  return;
}
signed main() {
  scanf("%lld %lld", &m, &n);
  for (long long i = (1); i <= (n); ++i)
    scanf("%lld", &a[i].first), a[i].second = i;
  for (long long i = (1); i <= (n); ++i)
    scanf("%lld", &b[i].first), b[i].second = i;
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (long long i = (1); i <= (n); ++i) ta[i] = a[i].first, tb[i] = b[i].first;
  work(ta, tb, 1);
  work(tb, ta, 2);
  for (long long i = (1); i <= (n); ++i) {
    sum[i] += sum[i - 1];
    if (minn > sum[i]) minn = sum[i], cnt = i;
  }
  printf("%lld\n", minn);
  for (long long i = (1); i <= (n); ++i) {
    ans[i] = make_pair(a[i].second, b[(i + cnt - 2) % n + 1].second);
  }
  sort(ans + 1, ans + n + 1);
  for (long long i = (1); i <= (n); ++i)
    printf("%lld%c", ans[i].second, " \n"[i == n]);
  return 0;
}
