#include <bits/stdc++.h>
using namespace std;
const long long N = 1000005, MOD = 1e9 + 7;
long long n, m, nn;
long long ANS;
long long ans[N];
pair<pair<long long, long long>, long long> P[N];
long long second[N];
deque<int> A, B;
long long p[N], s[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> m >> n;
  nn = n * 2;
  for (int i = 1; i <= n; i++) {
    cin >> P[i].first.first;
    P[i].first.second = 1;
    P[i].second = i;
  }
  for (int i = n + 1; i <= nn; i++) {
    cin >> P[i].first.first;
    P[i].first.second = -1;
    P[i].second = i - n;
  }
  sort(P + 1, P + nn + 1);
  long long x = n;
  for (int i = 1; i <= nn; i++) {
    x += P[i].first.second;
    second[x] += P[i % nn + 1].first.first - P[i].first.first;
    if (i == nn) second[x] += m;
  }
  long long ind = 0;
  ANS = 0;
  for (long long i = 1; i <= nn; i++) ANS += second[i] * i;
  p[0] = second[0];
  s[nn] = second[nn];
  for (int i = 1; i <= nn; i++) p[i] = p[i - 1] + second[i];
  for (int i = nn - 1; i >= 0; i--) s[i] = s[i + 1] + second[i];
  long long pas = ANS;
  for (long long i = 1; i <= nn; i++) {
    pas -= s[i];
    pas += p[i - 1];
    if (pas < ANS) {
      ANS = pas;
      ind = i;
    }
  }
  cout << ANS << endl;
  x = n;
  long long I = 0;
  for (int i = 0; i < nn; i++) {
    x += P[i].first.second;
    if (x == ind) {
      I = i + 1;
      break;
    }
  }
  for (int i = I; i < I + nn; i++) {
    int j = (i - 1) % nn + 1;
    if (P[j].first.second == 1) {
      if (B.size() == 0) {
        A.push_back(j);
        continue;
      }
      int x = B.front();
      B.pop_front();
      ans[P[j].second] = P[x].second;
    } else {
      if (A.size() == 0) {
        B.push_back(j);
        continue;
      }
      int x = A.front();
      A.pop_front();
      ans[P[x].second] = P[j].second;
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
