#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  long long M, N;
  cin >> M >> N;
  vector<long long> A(N), B(N);
  map<long long, long long> T;
  for (long long& i : A) {
    cin >> i;
    T[i]++;
  }
  for (long long& i : B) {
    cin >> i;
    T[i]--;
  }
  map<long long, long long> D{{0, 1}};
  long long L = 1;
  long long P = 0;
  for (auto [x, s] : T) {
    D[P] += x - L;
    P += s;
    L = x;
  }
  D[0] += M - L;
  long long k = 0, med = 0;
  for (auto [s, v] : D) {
    k += v;
    if (k > M / 2) {
      med = s;
      break;
    }
  }
  long long res = 0;
  for (auto [s, v] : D) {
    res += v * abs(s - med);
  }
  cout << res << '\n';
  long long pos = 0;
  P = 0;
  for (auto [x, s] : T) {
    if (P == med) {
      pos = x;
      break;
    }
    P += s;
  }
  for (long long& i : A) {
    i = (i - pos + M) % M + 1;
  }
  for (long long& i : B) {
    i = (i - pos + M) % M + 1;
  }
  vector<pair<long long, long long>> X(N), Y(N);
  for (long long i = 0; i < N; i++) {
    X[i] = make_pair(A[i], i);
    Y[i] = make_pair(B[i], i);
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  vector<long long> ans(N);
  for (long long i = 0; i < N; i++) {
    ans[X[i].second] = Y[i].second + 1;
  }
  for (long long i : ans) {
    cout << i << ' ';
  }
  cout << endl;
  return 0;
}
