#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e10;
const int maxn = 2e5 + 1000;
struct sa {
  long long val;
  int pos;
  bool operator<(sa a) const {
    if (val == a.val) return pos < a.pos;
    return val < a.val;
  }
} a[maxn], b[maxn * 3];
long long sum[maxn * 3];
long long pos[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m, n;
  cin >> m >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].val;
    a[i].pos = i;
  }
  sort(a + 1, a + n + 1);
  a[n + 1].val = inf;
  for (int i = 1; i <= n; ++i) {
    cin >> b[i].val;
    b[i].pos = i;
    b[i + n] = b[2 * n + i] = b[i];
    b[i].val -= m;
    b[2 * n + i].val += m;
  }
  sort(b + 1, b + 3 * n + 1);
  int p = 1;
  for (int i = 1; i <= n; ++i) {
    while (b[p].val <= a[i].val) ++p;
    sum[0] += a[i].val;
    sum[p - i + 1] -= 2 * a[i].val;
  }
  p = 1;
  for (int i = 1; i <= 3 * n; ++i) {
    while (a[p].val < b[i].val) ++p;
    sum[max(0, i - n + 1)] -= b[i].val;
    sum[i - p + 2] += 2 * b[i].val;
    sum[i + 1] -= b[i].val;
  }
  int ans = 1;
  for (int i = 1; i <= 2 * n + 1; ++i) {
    sum[i] += sum[i - 1];
    if (sum[i] < sum[ans]) {
      ans = i;
    }
  }
  cout << sum[ans] << endl;
  for (int i = 1; i <= n; ++i) {
    pos[a[i].pos] = b[ans + i - 1].pos;
  }
  for (int i = 1; i <= n; ++i) {
    cout << pos[i] << " ";
  }
  cout << endl;
  return 0;
}
