#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 50;
const int maxm = 1300000 + 100;
const int maxt = 3000000 + 100;
const int maxk = 10 + 3;
const int M = 2010;
const long long unit = 1LL;
const int INF = 0x3f3f3f3f;
const long long Inf = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-8;
const double inf = 1e15;
const double pi = acos(-1.0);
const long long mod = 998244353LL, gg = 3LL, inv2 = 499122177LL;
inline long long read() {
  long long x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') t = true, ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
struct Peo {
  int pos, id;
  bool operator<(const Peo &a) const {
    if (pos == a.pos) return id < a.id;
    return pos < a.pos;
  }
} peo[maxn];
struct City {
  int pos, id;
  bool operator<(const City &a) const {
    if (pos == a.pos) return id < a.id;
    return pos < a.pos;
  }
} city[maxn];
int m, n, a[maxn], b[maxn];
long long judge(int x) {
  long long ret = 0;
  if (x < 0) {
    for (int i = 0; i < -x; i++) ret += peo[i].pos + m - city[n + x + i].pos;
    for (int i = -x; i < n; i++) ret += abs(peo[i].pos - city[i + x].pos);
  } else {
    for (int i = 0; i < x; i++) ret += city[i].pos + m - peo[n - x + i].pos;
    for (int i = x; i < n; i++) ret += abs(city[i].pos - peo[i - x].pos);
  }
  return ret;
}
int main() {
  m = read(), n = read();
  for (int i = 0; i < n; ++i) city[i].pos = read(), city[i].id = i;
  sort(city, city + n);
  for (int i = 0; i < n; ++i) peo[i].pos = read(), peo[i].id = i;
  sort(peo, peo + n);
  int l = -n, r = n;
  while (l < r) {
    int m = (l + r) >> 1;
    if (judge(m) < judge(m + 1))
      r = m;
    else
      l = m + 1;
  }
  long long ans = Inf, anst = 0;
  for (int turn = l - 100; turn <= l + 100; ++turn) {
    int st = (turn % n + n) % n;
    long long res = 0;
    for (int i = 0; i < n; ++i) {
      res += min(abs(peo[i].pos - city[st].pos),
                 m - abs(peo[i].pos - city[st].pos));
      ++st;
      if (st >= n) st -= n;
    }
    if (res < ans) {
      ans = res;
      anst = turn;
    }
  }
  printf("%lld\n", ans);
  int st = (anst % n + n) % n;
  for (int i = 0; i < n; ++i) {
    b[city[st].id] = peo[i].id;
    ++st;
    if (st >= n) st -= n;
  }
  for (int i = 0; i < n; ++i) printf("%d ", b[i] + 1);
  return 0;
}
