#include <bits/stdc++.h>
using namespace std;
int m, n, bi, ans[200005];
pair<int, int> x[200005], y[200005];
long long cur, best = LLONG_MAX;
long long add[200005];
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x[i].first);
    x[i].second = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &y[i].first);
    y[i].second = i;
  }
  sort(x + 1, x + n + 1);
  sort(y + 1, y + n + 1);
  for (int i = 1, ta, tb, tc, td; i <= n; i++) {
    td = i;
    add[td - i + 1] += -x[i].first + m;
    ta = x[i].first - m / 2;
    tb = td, tc = n + 1;
    td = lower_bound(y + tb, y + tc, make_pair(ta, 0)) - y;
    add[td - i + 1] -= -x[i].first + m;
    add[td - i + 1] += x[i].first;
    ta = x[i].first + 1;
    tb = td, tc = n + 1;
    td = lower_bound(y + tb, y + tc, make_pair(ta, 0)) - y;
    add[td - i + 1] -= x[i].first;
    add[td - i + 1] += -x[i].first;
    ta = x[i].first + m / 2 + 1;
    tb = td, tc = n + 1;
    td = lower_bound(y + tb, y + tc, make_pair(ta, 0)) - y;
    add[td - i + 1] -= -x[i].first;
    add[td - i + 1] += x[i].first + m;
    td = n + 1;
    add[td - i + 1] -= x[i].first + m;
    add[td - i + 1] += -x[i].first + m;
    ta = x[i].first - m / 2;
    tb = 1, tc = i;
    td = lower_bound(y + tb, y + tc, make_pair(ta, 0)) - y;
    add[td + n - i + 1] -= -x[i].first + m;
    add[td + n - i + 1] += x[i].first;
    ta = x[i].first + 1;
    tb = td, tc = i;
    td = lower_bound(y + tb, y + tc, make_pair(ta, 0)) - y;
    add[td + n - i + 1] -= x[i].first;
    add[td + n - i + 1] += -x[i].first;
    ta = x[i].first + m / 2 + 1;
    tb = td, tc = i;
    td = lower_bound(y + tb, y + tc, make_pair(ta, 0)) - y;
    add[td + n - i + 1] -= -x[i].first;
    add[td + n - i + 1] += x[i].first + m;
    td = i + 1;
    add[i + 2 - td] += y[i].first;
    ta = y[i].first + m / 2 + 1;
    tb = 1, tc = td;
    td = lower_bound(x + tb, x + tc, make_pair(ta, 0)) - x;
    add[i + 2 - td] -= y[i].first;
    add[i + 2 - td] += -y[i].first;
    ta = y[i].first;
    tb = 1, tc = td;
    td = lower_bound(x + tb, x + tc, make_pair(ta, 0)) - x;
    add[i + 2 - td] -= -y[i].first;
    add[i + 2 - td] += y[i].first;
    ta = y[i].first - m / 2;
    tb = 1, tc = td;
    td = lower_bound(x + tb, x + tc, make_pair(ta, 0)) - x;
    add[i + 2 - td] -= y[i].first;
    add[i + 2 - td] += -y[i].first;
    td = 1;
    add[i + 2 - td] -= -y[i].first;
    add[i + 2 - td] += y[i].first;
    ta = y[i].first + m / 2 + 1;
    tb = i + 1, tc = n + 1;
    td = lower_bound(x + tb, x + tc, make_pair(ta, 0)) - x;
    add[i + 2 - td + n] -= y[i].first;
    add[i + 2 - td + n] += -y[i].first;
    ta = y[i].first;
    tb = i + 1, tc = td;
    td = lower_bound(x + tb, x + tc, make_pair(ta, 0)) - x;
    add[i + 2 - td + n] -= -y[i].first;
    add[i + 2 - td + n] += y[i].first;
    ta = y[i].first - m / 2;
    tb = i + 1, tc = td;
    td = lower_bound(x + tb, x + tc, make_pair(ta, 0)) - x;
    add[i + 2 - td + n] -= y[i].first;
    add[i + 2 - td + n] += -y[i].first;
  }
  for (int i = 1; i <= n; i++) {
    cur += add[i];
    if (cur < best) {
      bi = i;
      best = cur;
    }
  }
  printf("%lld\n", best);
  for (int i = 1; i <= n; i++) {
    int to = (i + bi - 2) % n + 1;
    ans[x[i].second] = y[to].second;
  }
  for (int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]);
}
