#include <bits/stdc++.h>
using namespace std;
const int maxn = 200007;
pair<long long, long long> a[2][maxn];
int L[maxn], R[maxn], m, n, M[maxn];
int suan(int x, int y) { return x <= y ? y - x + 1 : n - (x - y) + 1; }
long long sum[maxn];
void solve(int x, int y, long long val, int opt, int pos) {
  if (x > y) return;
  x = suan(pos, x);
  y = suan(pos, y);
  if (opt) {
    swap(x, y);
    if (x != 1) x = n + 2 - x;
    if (y != 1) y = n + 2 - y;
  }
  if (x <= y) {
    sum[x] += val;
    sum[y + 1] -= val;
  } else {
    sum[x] += val;
    sum[1] += val;
    sum[y + 1] -= val;
  }
}
int ed[maxn];
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first < b.first;
}
int main() {
  cin >> m >> n;
  for (int i = 1; i <= n; i++) cin >> a[0][i].first, a[0][i].second = i;
  for (int i = 1; i <= n; i++) cin >> a[1][i].first, a[1][i].second = i;
  sort(a[0] + 1, a[0] + n + 1);
  sort(a[1] + 1, a[1] + n + 1);
  for (int k = 0; k < 2; k++)
    for (int i = 1; i <= n; i++) {
      long long x = a[k][i].first;
      L[i] = lower_bound(a[k ^ 1] + 1, a[k ^ 1] + n + 1,
                         make_pair(x - m / 2, -1LL), cmp) -
             a[k ^ 1];
      R[i] = upper_bound(a[k ^ 1] + 1, a[k ^ 1] + n + 1,
                         make_pair(x + m / 2, -1LL), cmp) -
             a[k ^ 1] - 1;
      if (!k)
        M[i] = lower_bound(a[k ^ 1] + 1, a[k ^ 1] + n + 1, make_pair(x, -1LL),
                           cmp) -
               a[k ^ 1];
      else
        M[i] = upper_bound(a[k ^ 1] + 1, a[k ^ 1] + n + 1, make_pair(x, -1LL),
                           cmp) -
               a[k ^ 1];
      solve(1, L[i] - 1, m - x, k, i);
      solve(L[i], M[i] - 1, x, k, i);
      solve(M[i], R[i], -x, k, i);
      solve(R[i] + 1, n, x, k, i);
    }
  int pos = 0;
  for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
  pos = min_element(sum + 1, sum + n + 1) - sum;
  for (int i = 1; i <= n; i++)
    ed[a[0][i].second] = a[1][(i - 2 + pos + n) % n + 1].second;
  cout << sum[pos] << "\n";
  for (int i = 1; i <= n; i++) cout << ed[i] << " ";
  return 0;
}
