#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 239;
long long cnt(vector<long long> &a, long long l, long long r, long long x,
              long long y) {
  r++;
  return upper_bound(a.begin() + l, a.begin() + r, y) -
         lower_bound(a.begin() + l, a.begin() + r, x);
}
void solve(long long n, long long m, vector<long long> a, vector<long long> b,
           long long &ans, long long &best) {
  ans = INF;
  best = -1;
  for (long long i = 0; i < n; i++) {
    b.push_back(b[i]);
  }
  vector<long long> op(n);
  vector<long long> cl(n);
  auto add_segment = [&](long long l, long long r, long long val) {
    if (l < r) {
      op[l] += val;
      cl[r - 1] += val;
    }
  };
  auto relax_segment_a = [&](long long i, long long l, long long r) {
    if (l >= r) {
      return;
    }
    long long ff = cnt(b, l, r - 1, a[i] + m / 2 + 1, a[i] + m);
    long long ss = cnt(b, l, r - 1, a[i], a[i] + m / 2);
    long long tt = cnt(b, l, r - 1, a[i] - m / 2, a[i] - 1);
    long long dd = cnt(b, l, r - 1, a[i] - m, a[i] - m / 2 - 1);
    assert(ff + ss + tt + dd == r - l);
    long long st = l - i;
    add_segment(st, st + dd, m - a[i]);
    st += dd;
    add_segment(st, st + tt, a[i]);
    st += tt;
    add_segment(st, st + ss, -a[i]);
    st += ss;
    add_segment(st, st + ff, m + a[i]);
    st += ff;
  };
  auto relax_segment_b = [&](long long i, long long l, long long r) {
    if (l > r) {
      return;
    }
    long long st = i - r;
    long long ff = cnt(a, l, r, b[i] + m / 2 + 1, b[i] + m);
    long long ss = cnt(a, l, r, b[i] + 1, b[i] + m / 2);
    long long tt = cnt(a, l, r, b[i] - m / 2, b[i]);
    long long dd = cnt(a, l, r, b[i] - m, b[i] - m / 2 - 1);
    add_segment(st, st + ff, b[i]);
    st += ff;
    add_segment(st, st + ss, -b[i]);
    st += ss;
    add_segment(st, st + tt, b[i]);
    st += tt;
    add_segment(st, st + dd, -b[i]);
    st += dd;
  };
  for (long long i = 0; i < n; i++) {
    relax_segment_a(i, i, n);
    relax_segment_a(i, n, n + i);
  }
  for (long long i = 0; i < 2 * n; i++) {
    relax_segment_b(i, max((long long)0, i - (n - 1)), min(i, n - 1));
  }
  long long cur = 0;
  for (long long i = 0; i < n; i++) {
    cur += op[i];
    if (cur < ans) {
      ans = cur;
      best = i;
    }
    cur -= cl[i];
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long m, n;
  cin >> m >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  for (auto &t : a) {
    cin >> t;
  }
  for (auto &t : b) {
    cin >> t;
  }
  vector<long long> ind_a(n, 0);
  iota(ind_a.begin(), ind_a.end(), 0);
  sort(ind_a.begin(), ind_a.end(),
       [&](long long i, long long j) { return a[i] < a[j]; });
  vector<long long> ind_b(n, 0);
  iota(ind_b.begin(), ind_b.end(), 0);
  sort(ind_b.begin(), ind_b.end(),
       [&](long long i, long long j) { return b[i] < b[j]; });
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long ans, best;
  solve(n, m, a, b, ans, best);
  cout << ans << endl;
  rotate(ind_b.begin(), ind_b.begin() + best, ind_b.end());
  vector<long long> who(n);
  for (long long i = 0; i < n; i++) {
    who[ind_a[i]] = ind_b[i];
  }
  for (auto t : who) {
    cout << t + 1 << ' ';
  }
  cout << endl;
}
