#include <bits/stdc++.h>
using namespace std;
pair<int, int> A[200000], B[200000];
int P[200000];
long long res[200001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ((void)0);
  ((void)0);
  ((void)0);
  int N, M, j = 0, k = 0, l = 0;
  long long ans = 0x7fffffffffffffffLL;
  cin >> M >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i].first;
    A[i].second = B[i].second = i;
  }
  for (int i = 0; i < N; i++) cin >> B[i].first;
  sort(A, A + N);
  sort(B, B + N);
  for (int i = 0; i < N; i++) {
    while (j < N && A[i].first > B[j].first &&
           2 * (A[i].first - B[j].first) > M)
      j++;
    while (k < N && A[i].first > B[k].first) k++;
    while (l < N &&
           (A[i].first > B[l].first || 2 * (B[l].first - A[i].first) < M))
      l++;
    if (j) {
      res[(N + 1 + i - j) % N] += M - A[i].first;
      res[(i + 1) % N] -= M - A[i].first;
      if ((i + 1) % N <= (N + 1 + i - j) % N) res[0] += M - A[i].first;
    }
    if (j < k) {
      res[(N + 1 + i - k) % N] += A[i].first;
      res[(N + 1 + i - j) % N] -= A[i].first;
      if ((N + 1 + i - j) % N <= (N + 1 + i - k) % N) res[0] += A[i].first;
    }
    if (k < l) {
      res[(N + 1 + i - l) % N] -= A[i].first;
      res[(N + 1 + i - k) % N] += A[i].first;
      if ((N + 1 + i - k) % N <= (N + 1 + i - l) % N) res[0] -= A[i].first;
    }
    if (l < N) {
      res[(i + 1) % N] += M + A[i].first;
      res[(N + 1 + i - l) % N] -= M + A[i].first;
      if ((N + 1 + i - l) % N <= (i + 1) % N) res[0] += M + A[i].first;
    }
  }
  for (int i = j = k = l = 0; i < N; i++) {
    while (j < N && B[i].first >= A[j].first &&
           2 * (B[i].first - A[j].first) >= M)
      j++;
    while (k < N && B[i].first >= A[k].first) k++;
    while (l < N &&
           (B[i].first >= A[l].first || 2 * (A[l].first - B[i].first) <= M))
      l++;
    if (j) {
      res[(N - i) % N] -= B[i].first;
      res[(N + j - i) % N] += B[i].first;
      if ((N + j - i) % N <= (N - i) % N) res[0] -= B[i].first;
    }
    if (j < k) {
      res[(N + j - i) % N] += B[i].first;
      res[(N + k - i) % N] -= B[i].first;
      if ((N + k - i) % N <= (N + j - i) % N) res[0] += B[i].first;
    }
    if (k < l) {
      res[(N + k - i) % N] -= B[i].first;
      res[(N + l - i) % N] += B[i].first;
      if ((N + l - i) % N <= (N + k - i) % N) res[0] -= B[i].first;
    }
    if (l < N) {
      res[(N + l - i) % N] += B[i].first;
      res[(N - i) % N] -= B[i].first;
      if ((N - i) % N <= (N + l - i) % N) res[0] += B[i].first;
    }
  }
  for (int i = 0; i < N; i++) {
    res[i + 1] += res[i];
    ans = min(ans, res[i]);
  }
  cout << ans << '\n';
  for (int i = 0; i < N; i++)
    if (res[i] == ans) {
      for (int j = 0; j < N; j++) P[A[(i + j) % N].second] = B[j].second;
      break;
    }
  for (int i = 0; i < N; i++) cout << P[i] + 1 << ' ';
  return 0;
}
