#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  cin >> m >> n;
  vector<pair<int, int>> a, b;
  a.resize(n);
  for (int i = 0; i < n; ++i) {
    a[i].second = i;
    cin >> a[i].first;
  }
  sort(a.begin(), a.end());
  b.resize(n);
  for (int i = 0; i < n; ++i) {
    b[i].second = i;
    cin >> b[i].first;
  }
  sort(b.begin(), b.end());
  vector<long long> sum;
  sum.resize(n * 2 + 1);
  for (int i = 0; i < n * 2 + 1; ++i) {
    sum[i] = 0;
  }
  for (int i = 0, p0 = 0, p1 = 0, p2 = 0; i < n; ++i) {
    while (p0 < n && b[p0].first < a[i].first - 1.0 * m / 2) {
      ++p0;
    }
    while (p1 < n && b[p1].first < a[i].first) {
      ++p1;
    }
    while (p2 < n && b[p2].first < a[i].first + 1.0 * m / 2) {
      ++p2;
    }
    int beg = n - i;
    sum[beg + 0] += m - a[i].first;
    sum[beg + p0] -= m - a[i].first;
    sum[beg + p0] += a[i].first;
    sum[beg + p1] -= a[i].first;
    sum[beg + p1] += -a[i].first;
    sum[beg + p2] -= -a[i].first;
    sum[beg + p2] += m + a[i].first;
    sum[beg + n] -= m + a[i].first;
  }
  for (int i = 1; i < n * 2; ++i) {
    sum[i] += sum[i - 1];
  }
  for (int i = 0; i < n; ++i) {
    sum[i] += sum[i + n];
  }
  vector<long long> rsum;
  rsum.resize(n * 2 + 1);
  for (int i = 0; i < n * 2 + 1; ++i) {
    rsum[i] = 0;
  }
  for (int i = 0, p0 = 0, p1 = 0, p2 = 0; i < n; ++i) {
    while (p0 < n && a[p0].first <= b[i].first - 1.0 * m / 2) {
      ++p0;
    }
    while (p1 < n && a[p1].first <= b[i].first) {
      ++p1;
    }
    while (p2 < n && a[p2].first <= b[i].first + 1.0 * m / 2) {
      ++p2;
    }
    int beg = n - i - 1;
    rsum[beg + 0] += -b[i].first;
    rsum[beg + p0] -= -b[i].first;
    rsum[beg + p0] += b[i].first;
    rsum[beg + p1] -= b[i].first;
    rsum[beg + p1] += -b[i].first;
    rsum[beg + p2] -= -b[i].first;
    rsum[beg + p2] += b[i].first;
    rsum[beg + n] -= b[i].first;
  }
  for (int i = 1; i < n * 2; ++i) {
    rsum[i] += rsum[i - 1];
  }
  for (int i = 0; i < n; ++i) {
    rsum[i] += rsum[i + n];
  }
  vector<long long> cost;
  cost.resize(n);
  for (int i = 0; i < n; ++i) {
    cost[i] = sum[i] + rsum[n - 1 - i];
  }
  long long cur = (long long)(1e18);
  int pos;
  for (int i = 0; i < n; ++i) {
    if (cost[i] < cur) {
      cur = cost[i];
      pos = i;
    }
  }
  cout << cur << endl;
  vector<int> at;
  at.resize(n);
  for (int i = 0; i < n; ++i) {
    at[a[i].second] = b[(i + pos) % n].second;
  }
  for (int i = 0; i < n; ++i) {
    cout << at[i] + 1 << " ";
  }
  cout << endl;
  return 0;
}
