#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
int q1[N], q2[N];
int mat[N], n, m, top;
pair<int, int> a[N], q[N];
long long sum;
int op(int x) { return x < 0 ? -1 : 1; }
void push_mat(int x, int y) {
  if (x > y) swap(x, y);
  if (x == -n - 1 && y == n + 1) return;
  if (x == -n - 1)
    q1[++*q1] = y;
  else if (y == n + 1)
    q2[++*q2] = -x;
  else
    mat[y] = -x;
}
void push(pair<int, int> x) {
  if (top && op(q[top].second) != op(x.second)) {
    sum += x.first - q[top].first;
    push_mat(q[top].second, x.second);
    --top;
  } else
    q[++top] = x;
}
long long Func(int cnt) {
  top = *q1 = *q2 = sum = 0;
  if (cnt >= 0) {
    for (int i = (int)(1); i <= (int)(cnt); i++)
      push(pair<int, int>(0, -n - 1));
  } else {
    for (int i = (int)(1); i <= (int)(-cnt); i++)
      push(pair<int, int>(0, n + 1));
  }
  for (int i = (int)(1); i <= (int)(2 * n); i++) push(a[i]);
  if (cnt >= 0) {
    for (int i = (int)(1); i <= (int)(cnt); i++) push(pair<int, int>(m, n + 1));
  } else {
    for (int i = (int)(1); i <= (int)(-cnt); i++)
      push(pair<int, int>(m, -n - 1));
  }
  assert(*q1 == *q2);
  for (int i = (int)(1); i <= (int)(*q1); i++) mat[q1[i]] = q2[i];
  return sum;
}
int main() {
  scanf("%d%d", &m, &n);
  for (int i = (int)(1); i <= (int)(n); i++)
    scanf("%d", &a[i].first), a[i].second = i;
  for (int i = (int)(1); i <= (int)(n); i++)
    scanf("%d", &a[i + n].first), a[i + n].second = -i;
  sort(a + 1, a + 2 * n + 1);
  int l = -n, r = n;
  while (l < r) {
    int mid = (l + r + 2 * n) / 2 - n;
    if (Func(mid) <= Func(mid + 1))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", Func(l));
  for (int i = (int)(1); i <= (int)(n); i++) printf("%d ", mat[i]);
}
