#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  x = 0;
  char c = getchar();
  int flag = 0;
  while (c < '0' || c > '9') flag |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (flag) x = -x;
}
template <class T>
T _max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T _min(T a, T b) {
  return a < b ? a : b;
}
template <class T>
bool checkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
bool checkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
const int N = 200005;
int n, m, result[N];
int a[N], b[N];
long long ans[N];
struct node {
  int x;
  int id;
  bool operator<(const node &rhs) const { return x < rhs.x; }
} p[N], q[N];
void init() {
  read(m);
  read(n);
  for (int i = 1, tmp; i <= n; ++i) {
    read(tmp);
    p[i].x = tmp;
    p[i].id = i;
  }
  for (int i = 1, tmp; i <= n; ++i) {
    read(tmp);
    q[i].x = tmp;
    q[i].id = i;
  }
  sort(p + 1, p + n + 1);
  sort(q + 1, q + n + 1);
  for (int i = 1; i <= n; ++i) {
    a[i] = p[i].x;
    b[i] = q[i].x;
  }
}
int first_larger_or_equal(int *p, int x) {
  int tmp = lower_bound(p + 1, p + n + 1, x) - p;
  if (tmp <= n)
    return tmp;
  else
    return n + 1;
}
int last_smaller_or_equal(int *p, int x) {
  int tmp = upper_bound(p + 1, p + n + 1, x) - p;
  if (tmp <= n)
    return tmp - 1;
  else
    return n;
}
void add(int l, int r, int num) {
  ans[l] += num;
  ans[r + 1] -= num;
}
void update_a(int L, int R, int num, int idx) {
  if (L > R) return;
  int l = first_larger_or_equal(b, L);
  int r = last_smaller_or_equal(b, R);
  if (l > r) return;
  checkmax(l, 1);
  checkmin(r, n);
  if (l >= idx) {
    add(l - idx, r - idx, num);
  } else if (r < idx) {
    add(l - idx + n, r - idx + n, num);
  } else {
    add(0, r - idx, num);
    add(l - idx + n, n - 1, num);
  }
}
void update_b(int L, int R, int num, int idx) {
  if (L > R) return;
  int l = first_larger_or_equal(a, L);
  int r = last_smaller_or_equal(a, R);
  if (l > r) return;
  checkmax(l, 1);
  checkmin(r, n);
  if (r <= idx) {
    add(idx - r, idx - l, num);
  } else if (l > idx) {
    add(idx - r + n, idx - l + n, num);
  } else {
    add(0, idx - l, num);
    add(idx - r + n, n - 1, num);
  }
}
void solve() {
  for (int i = 1; i <= n; ++i) {
    int A = a[i];
    int pos1 = (A + A - m) / 2;
    int pos2 = ((long long)m + A + A + 1) / 2;
    update_a(1, pos1, m - A, i);
    update_a(pos1 + 1, A, A, i);
    update_a(pos2, m, m + A, i);
    update_a(A + 1, pos2 - 1, -A, i);
  }
  for (int i = 1; i <= n; ++i) {
    int B = b[i];
    int pos1 = ((long long)m + B + B - 1) / 2;
    int pos2 = (B + B - m) / 2;
    update_b(pos1 + 1, m, B, i);
    update_b(B, pos1, -B, i);
    update_b(1, pos2, -B, i);
    update_b(pos2 + 1, B - 1, B, i);
  }
  for (int i = 1; i < n; ++i) ans[i] += ans[i - 1];
  int pos = 0;
  long long value = ans[0];
  for (int i = 1; i < n; ++i) {
    if (checkmin(value, ans[i])) {
      pos = i;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (i + pos <= n)
      result[p[i].id] = q[i + pos].id;
    else
      result[p[i].id] = q[i + pos - n].id;
  }
  printf("%lld\n", value);
  for (int i = 1; i <= n; ++i) {
    printf("%d%c", result[i], " \n"[i == n]);
  }
}
int main() {
  init();
  solve();
  return 0;
}
