#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 998244353;
const ll root = 3;
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll modInv(ll a) { return binpow(a, MOD - 2); }
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 200001;
pair<ll, int> a[mxN];
pair<ll, int> b[3 * mxN];
int res[mxN];
int n;
ll m;
ll calc(int x) {
  ll res = 0;
  for (int i = (0); i < (n); ++i) {
    res += abs(a[i].first - b[i + x].first);
  }
  return res;
}
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin.tie(0)->sync_with_stdio(0);
  cin >> m >> n;
  for (int i = (0); i < (n); ++i) {
    cin >> a[i].first;
    a[i].first += m;
    a[i].second = i;
  }
  for (int i = (0); i < (n); ++i) {
    cin >> b[i].first;
    b[i + n] = {b[i].first + m, i};
    b[i + 2 * n] = {b[i].first + 2LL * m, i};
    b[i].second = i;
  }
  sort(a, a + n);
  sort(b, b + 3 * n);
  int l = 0;
  int r = 2 * n - 1;
  int ans = -1;
  while (l <= r) {
    int m = (l + r) / 2;
    if (calc(m) > calc(m + 1)) {
      l = m + 1;
      ans = m;
    } else
      r = m - 1;
  }
  ans++;
  cout << calc(ans) << "\n";
  for (int i = (0); i < (n); ++i) {
    res[a[i].second] = b[(i + ans) % n].second;
  }
  for (int i = (0); i < (n); ++i) {
    cout << res[i] + 1 << " ";
  }
  return 0;
}
