#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
using namespace std;
bool dbg = 0;
clock_t start_time = clock();
void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}
template <typename T>
string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}
template <typename T>
void amax(T& x, T y) {
  x = max(x, y);
}
template <typename T>
void amin(T& x, T y) {
  x = min(x, y);
}
template <typename T>
T input() {
  T ans = 0, m = 1;
  char c = ' ';
  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }
  if (c == '-') m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}
template <typename T>
void read(T& a) {
  a = input<T>();
}
template <typename T>
void read(T& a, T& b) {
  read(a), read(b);
}
template <typename T>
void read(T& a, T& b, T& c) {
  read(a, b), read(c);
}
template <typename T>
void read(T& a, T& b, T& c, T& d) {
  read(a, b), read(c, d);
}
const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)1e6 + 12;
const long long llinf = 2e18 + 5;
const double PI = acos(-1.0);
const int mod = 1e9 + 7;
template <typename T>
T binpow(T n, T second) {
  if (second <= 0) return 1LL;
  if (second % 2 == 0) {
    T b = binpow(n, second / 2);
    return (1LL * b * b) % mod;
  } else {
    return (1LL * binpow(n, second - 1) * n) % mod;
  }
}
bool distmode;
int dist(int m, int a, int b) {
  if (a == b) {
    return 0;
  }
  if (distmode == 0) {
    if (a < b)
      return b - a;
    else
      return a + m - b;
  } else {
    if (a > b)
      return a - b;
    else
      return a + m - b;
  }
}
int distb(int m, int a, int b) {
  if (a > b) swap(a, b);
  return min(b - a, a + m - b);
}
int brute(int m, vector<int> a, vector<int> b) {
  sort((b).begin(), (b).end());
  int res = inf;
  do {
    int cur = 0;
    for (int i = 0; i < a.size(); i++) {
      cur += distb(m, a[i], b[i]);
    }
    amin(res, cur);
  } while (next_permutation((b).begin(), (b).end()));
  return res;
}
void gen(int& m, vector<int>& a, vector<int>& b) {
  m = rand() % 10 + 5;
  int n = 6;
  a.clear();
  b.clear();
  for (int i = 0; i < n; i++) {
    a.push_back(rand() % m);
    b.push_back(rand() % m);
  }
}
int solve(int m, vector<int> a, vector<int> b) {
  vector<int> usa(a.size(), 0), usb(a.size(), 0);
  assert(a.size() == b.size());
  int res = 0;
  for (int i = 0; i < a.size(); i++) {
    int mn = inf, i1, i2;
    for (int j = 0; j < a.size(); j++)
      if (!usa[j])
        for (int k = 0; k < a.size(); k++)
          if (!usb[k]) {
            if (dist(m, a[j], b[k]) < mn) {
              mn = dist(m, a[j], b[k]);
              i1 = j;
              i2 = k;
            }
          }
    res += mn;
    usa[i1] = 1;
    usb[i2] = 1;
  }
  return res;
}
int solve2(int m, vector<int> a, vector<int> b) {
  vector<pair<int, int> > ev;
  for (auto e : a) {
    ev.push_back(make_pair(e, 0));
  }
  for (auto e : b) {
    ev.push_back(make_pair(e, 1));
  }
  sort((ev).begin(), (ev).end());
  int res = inf;
  vector<pair<int, int> > st;
  int mxdist = -inf;
  for (int i = 0; i < ev.size(); i++) {
    amax(mxdist, dist(m, ev[i].first, ev[(i + 1) % ev.size()].first));
  }
  distmode = 0;
  for (int i = 0; i < ev.size(); i++) {
    if (true) {
      int cur = 0;
      int ind = i;
      for (int j = 0; j < ev.size(); j++) {
        if (st.empty() || st.back().second == ev[ind].second) {
          st.push_back(ev[ind]);
        } else {
          cur += dist(m, st.back().first, ev[ind].first);
          st.pop_back();
        }
        ind += 1;
        if (ind == ev.size()) ind = 0;
      }
      amin(res, cur);
    }
  }
  distmode = 1;
  reverse((ev).begin(), (ev).end());
  for (int i = 0; i < ev.size(); i++) {
    if (true) {
      vector<pair<int, int> > st;
      int cur = 0;
      for (int j = 0; j < ev.size(); j++) {
        int ind = (j + i) % ev.size();
        if (st.empty() || st.back().second == ev[ind].second) {
          st.push_back(ev[ind]);
        } else {
          cur += dist(m, st.back().first, ev[ind].first);
          st.pop_back();
        }
      }
      amin(res, cur);
    }
  }
  return res;
}
struct mda {
  int first, second, id;
  bool operator<(const mda& t) const {
    return make_pair(first, second) < make_pair(t.first, t.second);
  }
};
long long solve3(int m, vector<int> a, vector<int> b, vector<int>& psans) {
  vector<mda> ev;
  for (int i = 0; i < a.size(); i++) {
    ev.push_back({a[i], -1, i});
  }
  for (int i = 0; i < a.size(); i++) {
    ev.push_back({b[i], 1, i});
  }
  sort((ev).begin(), (ev).end());
  map<int, long long> ccu, ccd;
  for (int i = 0, bal = 0; i < ev.size(); i++) {
    if (ev[i].second == -1)
      ccd[bal] += ev[i].first;
    else
      ccu[bal] += ev[i].first;
    bal += ev[i].second;
  }
  long long sa = 0, sb = 0;
  for (auto e : ccu) {
    if (e.first < 0) {
      sb += e.second;
    } else {
      sa += e.second;
    }
  }
  for (auto e : ccd) {
    if (e.first > 0) {
      sb += e.second;
    } else {
      sa += e.second;
    }
  }
  long long ans = sb - sa;
  int addToAll = 0;
  int res = 0;
  for (int i = 0; i + 1 < ev.size(); i++) {
    int rb = 0;
    if (ev[i].second == -1) {
      ccd[rb - addToAll] -= ev[i].first;
      sa -= ev[i].first;
      sb += ev[i].first + m;
      long long dg = ccu[-1 - addToAll];
      sb -= dg;
      sa += dg;
      long long dg2 = ccd[0 - addToAll];
      sa -= dg2;
      sb += dg2;
      ccd[+1 - addToAll] += ev[i].first + m;
    } else {
      ccu[rb - addToAll] -= ev[i].first;
      sa -= ev[i].first;
      sb += ev[i].first + m;
      long long dg = ccd[1 - addToAll];
      sb -= dg;
      sa += dg;
      long long dg2 = ccu[0 - addToAll];
      sa -= dg2;
      sb += dg2;
      ccu[-1 - addToAll] += ev[i].first + m;
    }
    addToAll -= ev[i].second;
    if (sb - sa < ans) {
      ans = sb - sa;
      res = i + 1;
    }
  }
  vector<mda> st;
  vector<int> ps(a.size());
  for (int i = 0; i < ev.size(); i++) {
    int ind = (res + i) % ev.size();
    if (st.empty() || st.back().second == ev[ind].second) {
      st.push_back(ev[ind]);
    } else {
      auto a = st.back();
      auto b = ev[ind];
      if (a.second == 1) swap(a, b);
      ps[a.id] = b.id;
      st.pop_back();
    }
  }
  psans = ps;
  return ans;
}
vector<int> ans;
long long solve4(int m, vector<int> a, vector<int> b) {
  vector<int> ps, ps2;
  long long kek = solve3(m, a, b, ps);
  for (int i = 0; i < a.size(); i++) {
    a[i] = m - a[i] - 1;
    b[i] = m - b[i] - 1;
  }
  long long mda = solve3(m, a, b, ps2);
  if (mda < kek) {
    ans = ps2;
  } else {
    ans = ps;
  }
  return min(mda, kek);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int m, n;
  cin >> m >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i]--;
  }
  cout << solve4(m, a, b) << "\n";
  for (auto x : ans) {
    cout << x + 1 << ' ';
  }
  return 0;
  int cnt = 0;
  const int it = 10000;
  for (int iter = 0; iter < it; iter++) {
    int m;
    vector<int> a, b;
    gen(m, a, b);
    int ans1, ans2;
    if ((ans1 = brute(m, a, b)) != (ans2 = solve4(m, a, b))) {
      cnt++;
      cout << ans1 << ' ' << ans2 << '\n';
      cout << m << '\n';
      for (auto e : a) cout << e << ',';
      cout << '\n';
      for (auto e : b) cout << e << ',';
      exit(0);
    }
  }
  cout << 1. * cnt / it << '\n';
  if (cnt == 0) cout << "ok";
  return 0;
}
