#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const long long mod = 1e9 + 7;
int m, n, bf = 200000;
struct mmp {
  int x, p, s, id;
  bool operator<(const mmp &op) const { return x < op.x; }
} a[maxn];
long long ss, f[maxn], ans;
int mi, sp, h[maxn];
queue<int> q;
void join(int x, int y) {
  if (x > n / 2)
    h[y] = x - n / 2;
  else
    h[x] = y - n / 2;
}
int main() {
  int i, j;
  cin >> m >> n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i].x);
    a[i].p = 1;
    a[i].id = i;
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i + n].x);
    a[i + n].p = -1;
    a[i + n].id = i + n;
  }
  n *= 2;
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    a[i].s += a[i].p + a[i - 1].s;
    if ((a[i].p < 0 && a[i].s >= 0) || (a[i].p > 0 && a[i].s <= 0)) {
      ss += a[i].x;
      if (a[i].p > 0)
        f[a[i].s + bf] += a[i].x;
      else
        f[a[i].s + bf + 1] += a[i].x;
    } else {
      ss -= a[i].x;
      if (a[i].p > 0)
        f[a[i].s + bf] -= a[i].x;
      else
        f[a[i].s + bf + 1] -= a[i].x;
    }
  }
  ans = ss;
  mi = 1;
  for (i = 1; i < n; i++) {
    if (a[i].p > 0) {
      f[a[i].s + bf] *= -1;
      ss += f[a[i].s + bf] * 2;
      f[a[i].s + bf] += m;
      ss += m;
    } else {
      f[a[i].s + bf + 1] *= -1;
      ss += f[a[i].s + bf + 1] * 2;
      f[a[i].s + bf + 1] += m;
      ss += m;
    }
    if (ss < ans) {
      ans = ss;
      mi = i + 1;
    }
  }
  cout << ans << "\n";
  for (i = mi; i <= n; i++) {
    sp += a[i].p;
    if ((a[i].p < 0 && sp >= 0) || (a[i].p > 0 && sp <= 0)) {
      join(q.front(), a[i].id);
      q.pop();
    } else
      q.push(a[i].id);
  }
  for (i = 1; i < mi; i++) {
    sp += a[i].p;
    if ((a[i].p < 0 && sp >= 0) || (a[i].p > 0 && sp <= 0)) {
      join(q.front(), a[i].id);
      q.pop();
    } else
      q.push(a[i].id);
  }
  for (i = 1; i <= n / 2; i++) printf("%d ", h[i]);
  cout << "\n";
  return 0;
}
