#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
int m, n, x, pre[N], id, st1[N], st2[N], Ans[N];
long long s1[N], s2[N];
pair<int, int> a[N];
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    a[i] = make_pair(x, i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    a[i + n] = make_pair(x, i + n);
  }
  std::sort(a + 1, a + 2 * n + 1);
  pre[0] = n;
  int p = 0, tg = n;
  long long sum = 0;
  for (int i = 1; i <= 2 * n; i++) {
    if (a[i].second <= n) {
      pre[i] = pre[i - 1] + 1;
      s1[pre[i]] += a[i].first;
      sum += pre[i] > tg ? -a[i].first : a[i].first;
    } else {
      pre[i] = pre[i - 1] - 1;
      s2[pre[i]] += a[i].first;
      sum += pre[i] >= tg ? a[i].first : -a[i].first;
    }
  }
  long long ans = sum;
  id = 1;
  for (int i = 1; i < 2 * n; i++) {
    if (a[i].second <= n) {
      sum -= 2 * s2[tg];
      tg++;
      sum += 2 * s1[tg];
      s1[pre[i]] -= a[i].first;
      s1[tg] += a[i].first + m;
    } else {
      sum -= 2 * s1[tg];
      tg--;
      sum += 2 * s2[tg];
      s2[pre[i]] -= a[i].first;
      s2[tg] += a[i].first + m;
    }
    sum += m;
    if (sum < ans) ans = sum, id = i + 1;
  }
  printf("%lld\n", ans);
  for (int i = id, tp1 = 0, tp2 = 0, cnt = 1; cnt <= 2 * n; i++, cnt++) {
    if (i > 2 * n) i = 1;
    if (a[i].second <= n) {
      if (tp2)
        Ans[a[i].second] = st2[tp2--];
      else
        st1[++tp1] = a[i].second;
    } else {
      if (tp1)
        Ans[st1[tp1--]] = a[i].second - n;
      else
        st2[++tp2] = a[i].second - n;
    }
  }
  for (int i = 1; i <= n; i++) printf("%d ", Ans[i]);
}
