#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 6e5 + 7;
const long long INF = 1e18 + 7;
pair<long long, long long> a[N], b[N];
int n, m, us[N];
long long solve(int x) {
  long long s = 0;
  for (int i = 1; i <= n; i++) s += abs(a[i + x].first - b[i].first);
  return s;
}
int main() {
  cin >> m >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].first);
    a[i].second = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i].first);
    b[i].second = i;
  }
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= 3 * n; i++) {
    if (i <= n)
      a[i].first -= m;
    else {
      a[i] = a[i - n];
      a[i].first += m;
    }
  }
  int l = 0, r = 2 * n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (solve(mid) < solve(mid + 1))
      r = mid;
    else
      l = mid + 1;
  }
  cout << solve(l) << endl;
  for (int i = 1; i <= n; i++) us[a[i + l].second] = b[i].second;
  for (int i = 1; i <= n; i++) cout << us[i] << ' ';
}
