#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long M, N;
  cin >> M >> N;
  vector<long long> A(N);
  vector<long long> B(N);
  long long res = 0;
  vector<pair<long long, pair<long long, long long> > > vp;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    vp.emplace_back(A[i], make_pair(0, i));
  }
  for (long long i = 0; i < N; i++) {
    cin >> B[i];
    vp.emplace_back(B[i], make_pair(1, i));
  }
  sort(vp.begin(), vp.end());
  for (long long i = 0; i < 2 * N; i++) {
    vp.push_back(vp[i]);
    vp.back().first += M;
  }
  vector<long long> rs(4 * N + 5, -1);
  vector<long long> cur(4 * N + 5, -1);
  vector<long long> sum(4 * N + 5, 0);
  vector<vector<pair<long long, long long> > > idx(4 * N + 5);
  vector<long long> al(4 * N + 5, 0);
  long long s = 2 * N + 2;
  for (long long i = 0; i < vp.size(); i++) {
    long long pos = vp[i].first;
    if (vp[i].second.first == 1) {
      s--;
    }
    if (rs[s] == -1) {
      rs[s] = pos;
    }
    if (pos <= rs[s] + M) {
      idx[s].push_back(vp[i].second);
      if (cur[s] == -1) {
        cur[s] = pos;
      } else {
        sum[s] += pos - cur[s];
        cur[s] = -1;
      }
    }
    if (vp[i].second.first == 0) {
      s++;
    }
  }
  long long INF = (long long)1 << 60;
  res = INF;
  long long ans = 0;
  for (long long i = 0; i < sum.size(); i++) {
    ans += sum[i];
  }
  long long mni = -1;
  res = ans;
  for (long long i = 0; i < vp.size(); i++) {
    long long pos = vp[i].first;
    if (vp[i].second.first == 1) {
      s--;
    }
    ans -= sum[s];
    sum[s] = M - sum[s];
    ans += sum[s];
    if (res > ans) {
      mni = i;
      res = ans;
    }
    if (vp[i].second.first == 0) {
      s++;
    }
  }
  for (long long i = 0; i < vp.size(); i++) {
    if (i > mni) break;
    long long pos = vp[i].first;
    if (vp[i].second.first == 1) {
      s--;
    }
    al[s] ^= 1;
    if (vp[i].second.first == 0) {
      s++;
    }
  }
  vector<long long> X(N);
  for (long long i = 0; i < idx.size(); i++) {
    for (long long j = al[i]; j + 1 < idx[i].size(); j += 2) {
      if (idx[i][j].first == 0) {
        X[idx[i][j].second] = idx[i][j + 1].second;
      } else {
        X[idx[i][j + 1].second] = idx[i][j].second;
      }
    }
  }
  cout << res << endl;
  for (long long i = 0; i < N; i++) {
    if (i > 0) cout << " ";
    cout << X[i] + 1;
  }
  cout << endl;
}
