#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
int len, n;
int a[MAXN];
int b[MAXN];
int ans[MAXN];
int abound[MAXN][3];
int bbound[MAXN][3];
pair<int, int> o[MAXN];
int mpa[MAXN];
int mpb[MAXN];
long long solve() {
  for (int i = (0); i < (n); ++i) o[i] = make_pair(a[i], i);
  sort(o, o + n);
  for (int i = (0); i < (n); ++i) mpa[i] = o[i].second, a[i] = o[i].first;
  for (int i = (0); i < (n); ++i) o[i] = make_pair(b[i], i);
  sort(o, o + n);
  for (int i = (0); i < (n); ++i) mpb[i] = o[i].second, b[i] = o[i].first;
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j < (3); ++j)
      abound[i][j] = i == 0 ? 0 : abound[i - 1][j];
    while (abound[i][0] < n && 2 * (a[i] - b[abound[i][0]]) >= len)
      ++abound[i][0];
    while (abound[i][1] < n && b[abound[i][1]] <= a[i]) ++abound[i][1];
    while (abound[i][2] < n && 2 * (b[abound[i][2]] - a[i]) < len)
      ++abound[i][2];
  }
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j < (3); ++j)
      bbound[i][j] = i == 0 ? 0 : bbound[i - 1][j];
    while (bbound[i][0] < n && 2 * (b[i] - a[bbound[i][0]]) >= len)
      ++bbound[i][0];
    while (bbound[i][1] < n && a[bbound[i][1]] < b[i]) ++bbound[i][1];
    while (bbound[i][2] < n && 2 * (a[bbound[i][2]] - b[i]) < len)
      ++bbound[i][2];
  }
  long long cur = 0;
  vector<long long> delta(n, 0);
  for (int i = (0); i < (n); ++i) {
    int w = 0, x = abound[i][0], y = abound[i][1], z = abound[i][2];
    if (w <= i && i < x) cur += len - (a[i] - b[i]);
    if (x <= i && i < y) cur += a[i] - b[i];
    if (y <= i && i < z) cur += b[i] - a[i];
    if (z <= i && i < n) cur += len - (b[i] - a[i]);
    {
      int t = (x - i + n) % n;
      delta[t] += 2 * a[i] - (long long)len;
    }
    {
      int t = (y - i + n) % n;
      delta[t] -= 2 * a[i];
    }
    {
      int t = (z - i + n) % n;
      delta[t] += 2 * a[i] + (long long)len;
    }
    {
      int t = (w - i + n) % n;
      delta[t] -= 2 * a[i];
    }
  }
  for (int i = (0); i < (n); ++i) {
    int w = 0, x = bbound[i][0], y = bbound[i][1], z = bbound[i][2];
    {
      int t = (i - x + 1 + n) % n;
      delta[t] -= 2 * b[i];
    }
    {
      int t = (i - y + 1 + n) % n;
      delta[t] += 2 * b[i];
    }
    {
      int t = (i - z + 1 + n) % n;
      delta[t] -= 2 * b[i];
    }
    {
      int t = (i - w + 1 + n) % n;
      delta[t] += 2 * b[i];
    }
  }
  long long ret = cur;
  int offset = 0;
  for (int i = (1); i < (n); ++i) {
    cur += delta[i];
    if (cur < ret) ret = cur, offset = i;
  }
  for (int i = (0); i < (n); ++i) ans[mpa[i]] = mpb[(i + offset) % n];
  return ret;
}
void run() {
  scanf("%d%d", &len, &n);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]), --a[i];
  for (int i = (0); i < (n); ++i) scanf("%d", &b[i]), --b[i];
  printf("%lld\n", solve());
  for (int i = (0); i < (n); ++i) {
    if (i != 0) printf(" ");
    printf("%d", ans[i] + 1);
  }
  puts("");
}
int main() {
  run();
  return 0;
}
