#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
using pii = pair<int, int>;
int m, n;
long long d[N * 2];
pii a[N], b[N];
void add(pii *q, int i, int l, int r, int v, int tp = 0) {
  int pl = lower_bound(q, q + n, pii(l, -1)) - q,
      pr = lower_bound(q, q + n, pii(r + 1, -1)) - q - 1;
  if (pl > pr) return;
  if (tp) {
    swap(pl, pr);
    pl = i - pl;
    pr = i - pr;
  } else {
    pl -= i;
    pr -= i;
  }
  if (pl >= 0 && pr >= 0)
    d[pl] += v, d[pr + 1] -= v;
  else if (pl < 0 && pr < 0)
    d[pl + n] += v, d[pr + n + 1] -= v;
  else {
    d[pl + n] += v;
    d[n] -= v;
    d[0] += v;
    d[pr + 1] -= v;
  }
}
int ans[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> m >> n;
  for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i;
  for (int i = 0; i < n; i++) cin >> b[i].first, b[i].second = i;
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; i++) {
    add(b, i, a[i].first - m, a[i].first - m / 2 - 1, m - a[i].first);
    add(b, i, a[i].first - m / 2, a[i].first - 1, a[i].first);
    add(b, i, a[i].first, a[i].first + m / 2, -a[i].first);
    add(b, i, a[i].first + m / 2 + 1, a[i].first + m, a[i].first + m);
  }
  for (int i = 0; i < n; i++) {
    add(a, i, b[i].first - m, b[i].first - m / 2 - 1, -b[i].first, 1);
    add(a, i, b[i].first - m / 2, b[i].first, b[i].first, 1);
    add(a, i, b[i].first + 1, b[i].first + m / 2, -b[i].first, 1);
    add(a, i, b[i].first + m / 2 + 1, b[i].first + m, b[i].first, 1);
  }
  for (int i = 1; i <= n; i++) d[i] += d[i - 1];
  int mn = min_element(d, d + n) - d;
  for (int i = 0; i < n; i++) ans[a[i].second] = b[(i + mn) % n].second;
  cout << d[mn] << endl;
  for (int i = 0; i < n; i++) cout << ans[i] + 1 << ' ';
  return 0;
}
