#include <bits/stdc++.h>
using namespace std;
void Main();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  while (~cin.peek()) Main(), cin.get();
  return 0;
}
void Main() {
  int m, n;
  cin >> m >> n;
  vector<pair<int, int>> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i].first;
    b[i].second = i;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  vector<long long> c(n + 1);
  auto add_ans = [&](int l, int r, int x, int mark = 0) {
    if (l > r) return;
    if (l < 0) l += n;
    if (r < 0) r += n;
    int ll = l, rr = r;
    if (mark) {
      ll = (n - r) % n;
      rr = (n - l) % n;
    }
    c[ll] += x;
    c[rr + 1] -= x;
    if (ll > rr) {
      c[n] -= x;
      c[0] += x;
    }
  };
  auto add = [&](int p, vector<pair<int, int>> &a, int v1, int v2, int v3,
                 int x1, int x2, int x3, int x4, int mark = 0) {
    int p1 = lower_bound(a.begin(), a.end(), make_pair(v1, -1)) - a.begin();
    int p2 = lower_bound(a.begin(), a.end(), make_pair(v2, -1)) - a.begin();
    int p3 = lower_bound(a.begin(), a.end(), make_pair(v3, -1)) - a.begin();
    add_ans(-p, p1 - p - 1, x1, mark);
    add_ans(p1 - p, p2 - p - 1, x2, mark);
    add_ans(p2 - p, p3 - p - 1, x3, mark);
    add_ans(p3 - p, n - p - 1, x4, mark);
  };
  for (int i = 0; i < n; i++) {
    add(i, b, a[i].first - m / 2, a[i].first, a[i].first + m / 2 + 1,
        -a[i].first + m, a[i].first, -a[i].first, a[i].first + m);
    add(i, a, b[i].first - m / 2, b[i].first + 1, b[i].first + m / 2 + 1,
        -b[i].first, b[i].first, -b[i].first, b[i].first, 1);
  }
  for (int i = 1; i < n; i++) c[i] += c[i - 1];
  int id = min_element(c.begin(), c.begin() + n) - c.begin();
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    ans[a[i].second] = b[(i + id) % n].second;
  }
  cout << c[id] << endl;
  for (int i = 0; i < n; i++) {
    cout << ans[i] + 1 << " \n"[i == n - 1];
  }
}
