#include <bits/stdc++.h>
using namespace std;
const int maxn = 800001;
struct seg {
  int len, d;
  seg() {}
  seg(int len, int d) : len(len), d(d) {}
  bool operator<(const seg &s) const {
    if (d != s.d) return d < s.d;
    return len < s.len;
  }
};
struct vec {
  int a, type, id;
  vec() {}
  vec(int a, int type, int id) : a(a), type(type), id(id) {}
  bool operator<(const vec &v) const { return a < v.a; }
};
vec ord[maxn];
int n, m;
map<int, long long> sum;
long long spos, sneg;
int delta;
long long cur, ans;
int curd[maxn];
int pa[maxn], pb[maxn], p[maxn];
void insert(int len, int d);
void remove(int len, int d);
void increase();
void decrease();
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    ord[i] = vec(a, 1, i);
  }
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    ord[i + n] = vec(a, -1, i);
  }
  sort(ord + 1, ord + 1 + 2 * n);
  for (int i = 1; i < 2 * n; i++) {
    curd[i] = curd[i - 1] + ord[i].type;
    int len = ord[i + 1].a - ord[i].a;
    insert(len, curd[i]);
  }
  ans = cur;
  int minid = 0;
  for (int i = 1; i < 2 * n; i++) {
    remove(ord[i + 1].a - ord[i].a, curd[i] + delta);
    if (ord[i].type == 1)
      decrease();
    else
      increase();
    ord[i + 2 * n] = vec(ord[i].a + m, ord[i].type, ord[i].id);
    int len = ord[i + 2 * n].a - ord[i + 2 * n - 1].a;
    if (ord[i].type == 1)
      insert(len, -1);
    else
      insert(len, 1);
    if (cur < ans) {
      ans = cur;
      minid = i;
    }
  }
  printf("%lld\n", ans);
  int cnta = 0;
  int cntb = 0;
  for (int i = minid + 1; i <= minid + 2 * n; i++) {
    if (ord[i].type == 1)
      pa[++cnta] = ord[i].id;
    else
      pb[++cntb] = ord[i].id;
  }
  for (int i = 1; i <= n; i++) {
    p[pa[i]] = pb[i];
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", p[i]);
  }
  return 0;
}
void insert(int len, int d) {
  sum[d - delta] += len;
  if (d >= 0) {
    spos += len;
    cur += (long long)d * len;
  } else {
    sneg += len;
    cur += (long long)(-d) * len;
  }
}
void remove(int len, int d) {
  sum[d - delta] -= len;
  if (d >= 0) {
    spos -= len;
    cur -= (long long)d * len;
  } else {
    sneg -= len;
    cur -= (long long)(-d) * len;
  }
}
void increase() {
  delta++;
  cur += spos;
  cur -= sneg;
  spos += sum[-delta];
  sneg -= sum[-delta];
}
void decrease() {
  spos -= sum[-delta];
  sneg += sum[-delta];
  delta--;
  cur -= spos;
  cur += sneg;
}
