#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = false;
  for (; !isdigit(c); c = getchar()) f |= c == '-';
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  if (f) x = -x;
}
template <typename T, typename... Arg>
inline void read(T &x, Arg &...args) {
  read(x);
  read(args...);
}
template <typename T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 6e5 + 100;
int n;
long long m;
pair<int, int> x[N], y[N];
int a[N], b[N];
long long delta[N];
int ans[N];
void add(int l, int r, int k) { delta[l] += k, delta[r + 1] -= k; }
signed main() {
  read(m, n);
  for (int i = 1; i <= n; i++) read(x[i].first), x[i].second = i;
  sort(x + 1, x + n + 1);
  for (int i = 1; i <= n; i++) read(y[i].first), y[i].second = i;
  sort(y + 1, y + n + 1);
  for (int i = 1; i <= n; i++) b[i] = x[i].first - m;
  for (int i = 1; i <= n; i++) b[i + n] = x[i].first;
  for (int i = 1; i <= n; i++) b[i + n + n] = x[i].first + m;
  for (int i = 1; i <= n; i++) a[i] = y[i].first, delta[0] += a[i];
  for (int i = 1; i <= n; i++) {
    int j = upper_bound(b + 1, b + 1 + 3 * n, a[i]) - b;
    if (j > i && j <= i + 2 * n) delta[j - i] -= 2 * a[i];
  }
  for (int i = 1; i <= 3 * n; i++) {
    if (i <= n) {
      add(0, i - 1, -b[i]);
      continue;
    }
    if (i > n * 2) {
      add(i - n, n * 2, b[i]);
      continue;
    }
    int j = lower_bound(a + 1, a + 1 + n, b[i]) - a;
    add(i - j + 1, i - 1, b[i]);
    add(i - n, i - j, -b[i]);
  }
  pair<long long, int> _ = {delta[0], 0};
  for (int i = 1; i <= 2 * n; i++) {
    delta[i] += delta[i - 1];
    _ = min(_, make_pair(delta[i], i));
  }
  write(_.first);
  putchar('\n');
  int id = _.second;
  for (int i = 1; i <= n; i++)
    ans[x[(i + id - 1) % n + 1].second] = y[i].second;
  for (int i = 1; i <= n; i++) write(ans[i]), putchar(' ');
}
