#include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
pair<long long, int> A[MX], B[3 * MX];
int n;
long long calc(int x) {
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += abs(A[i].first - B[i + x].first);
  }
  return sum;
}
int with[MX];
int main() {
  int m;
  ignore = scanf("%d %d", &m, &n);
  for (int i = 0; i < n; i++) {
    int x;
    ignore = scanf("%d", &x);
    A[i] = {x + m, i};
  }
  for (int i = 0; i < n; i++) {
    int x;
    ignore = scanf("%d", &x);
    B[i] = {x, i};
    B[i + n] = {x + m * 1LL, i};
    B[i + 2 * n] = {x + m * 2LL, i};
  }
  sort(A, A + n);
  sort(B, B + 3 * n);
  int L = 0, R = 2 * n - 1;
  int ans = -1;
  while (L <= R) {
    int M = (L + R) / 2;
    if (calc(M + 1) < calc(M)) {
      ans = M;
      L = M + 1;
    } else {
      R = M - 1;
    }
  }
  ans++;
  for (int i = 0; i < n; i++) {
    with[A[i].second] = B[i + ans].second;
  }
  printf("%lld\n", calc(ans));
  for (int i = 0; i < n; i++) {
    printf("%d ", with[i] + 1);
  }
  printf("\n");
}
