#include <bits/stdc++.h>
using namespace std;
int main() {
  long long m, n;
  cin >> m >> n;
  vector<pair<long long, int> > arr(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    cin >> arr[i].first;
    arr[i].second = i;
  }
  sort(arr.begin(), arr.end());
  vector<vector<int> > brr(2 * n);
  int temp = n;
  for (int i = 0; i < 2 * n; i++) {
    if (arr[i].second < n)
      brr[temp++].push_back(i);
    else
      brr[--temp].push_back(i);
  }
  long long x = 0, temp1, temp2;
  vector<long long> ans(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    if (brr[i].size() == 0) continue;
    temp1 = 0;
    for (int j = 0; j < brr[i].size(); j = j + 2)
      temp1 = temp1 + arr[brr[i][j + 1]].first - arr[brr[i][j]].first;
    temp2 = arr[brr[i][0]].first - arr[brr[i].back()].first + m;
    for (int j = 1; j < brr[i].size() - 1; j = j + 2)
      temp2 = temp2 + arr[brr[i][j + 1]].first - arr[brr[i][j]].first;
    x = x + min(temp1, temp2);
    for (int j = 0; j < brr[i].size(); j++) {
      if (temp1 < temp2)
        ans[arr[brr[i][j]].second] = arr[brr[i][j ^ 1]].second;
      else {
        if (j == 0)
          ans[arr[brr[i][j]].second] = arr[brr[i].back()].second;
        else if (j == brr[i].size() - 1)
          ans[arr[brr[i][j]].second] = arr[brr[i][0]].second;
        else
          ans[arr[brr[i][j]].second] = arr[brr[i][((j - 1) ^ 1) + 1]].second;
      }
    }
  }
  cout << x << endl;
  for (int i = 0; i < n; i++) cout << ans[i] - n + 1 << " ";
  cout << endl;
}
