#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
long long ans[N], ans2[N], d[N];
void add(long long l, long long r, long long delta) {
  d[l] += delta;
  d[r + 1] -= delta;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long m, n;
  cin >> m >> n;
  vector<pair<long long, long long>> a(n), b(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i + 1;
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i].first;
    b[i].second = i + 1;
  }
  sort((a).begin(), (a).end());
  sort((b).begin(), (b).end());
  vector<long long> ans(n), ans2(n);
  for (long long i = 0; i < n; i++) {
    long long mx = -1;
    if (b[i].first - a[0].first > m / 2) {
      long long to = lower_bound((a).begin(), (a).end(),
                                 make_pair(b[i].first - m / 2, 0ll)) -
                     a.begin() - 1;
      long long cl = n - i, cr = to - i;
      if (cr < 0) {
        cr += n;
      }
      if (cl <= cr) {
        add(cl, cr, m - b[i].first);
      } else {
        add(cl, n - 1, m - b[i].first);
        add(0, cr, m - b[i].first);
      }
      mx = to;
    }
    if (mx + 1 < n && b[i].first >= a[mx + 1].first) {
      long long to =
          upper_bound((a).begin(), (a).end(), make_pair(b[i].first + 1, -1ll)) -
          a.begin() - 1;
      if (mx + 1 <= to) {
        long long cl = (mx + 1) - i, cr = to - i;
        if (cl < 0) {
          cl += n;
        }
        if (cr < 0) {
          cr += n;
        }
        if (cl <= cr) {
          add(cl, cr, b[i].first);
        } else {
          add(cl, n - 1, b[i].first);
          add(0, cr, b[i].first);
        }
        mx = to;
      }
    }
    if (mx + 1 < n && a[mx + 1].first - b[i].first <= m / 2) {
      long long to = lower_bound((a).begin(), (a).end(),
                                 make_pair(b[i].first + m / 2 + 1, 0ll)) -
                     a.begin() - 1;
      if (mx + 1 <= to) {
        long long cl = (mx + 1) - i, cr = to - i;
        if (cl < 0) {
          cl += n;
        }
        if (cr < 0) {
          cr += n;
        }
        if (cl <= cr) {
          add(cl, cr, -b[i].first);
        } else {
          add(cl, n - 1, -b[i].first);
          add(0, cr, -b[i].first);
        }
        mx = to;
      }
    }
    if (mx + 1 < n) {
      long long to = n - 1;
      if (mx + 1 <= to) {
        long long cl = (mx + 1) - i, cr = to - i;
        if (cl < 0) {
          cl += n;
        }
        if (cr < 0) {
          cr += n;
        }
        if (cl <= cr) {
          add(cl, cr, m + b[i].first);
        } else {
          add(cl, n - 1, m + b[i].first);
          add(0, cr, m + b[i].first);
        }
        mx = to;
      }
    }
  }
  swap(a, b);
  for (long long i = 0; i < n; i++) {
    long long mx = -1;
    if (b[i].first - a[0].first > m / 2) {
      long long to = lower_bound((a).begin(), (a).end(),
                                 make_pair(b[i].first - m / 2, 0ll)) -
                     a.begin() - 1;
      long long cl = n - i, cr = to - i;
      cl *= -1, cr *= -1;
      if (cl < 0) {
        cl += n;
      }
      if (cr < 0) {
        cr += n;
      }
      swap(cl, cr);
      if (cl <= cr) {
        add(cl, cr, -b[i].first);
      } else {
        add(cl, n - 1, -b[i].first);
        add(0, cr, -b[i].first);
      }
      mx = to;
    }
    if (mx + 1 < n && b[i].first > a[mx + 1].first) {
      long long to =
          upper_bound((a).begin(), (a).end(), make_pair(b[i].first, -1ll)) -
          a.begin() - 1;
      if (mx + 1 <= to) {
        long long cl = (mx + 1) - i, cr = to - i;
        cl *= -1, cr *= -1;
        if (cl < 0) {
          cl += n;
        }
        if (cr < 0) {
          cr += n;
        }
        swap(cl, cr);
        if (cl <= cr) {
          add(cl, cr, b[i].first);
        } else {
          add(cl, n - 1, b[i].first);
          add(0, cr, b[i].first);
        }
        mx = to;
      }
    }
    if (mx + 1 < n && a[mx + 1].first - b[i].first <= m / 2) {
      long long to = lower_bound((a).begin(), (a).end(),
                                 make_pair(b[i].first + m / 2 + 1, 0ll)) -
                     a.begin() - 1;
      if (mx + 1 <= to) {
        long long cl = (mx + 1) - i, cr = to - i;
        cl *= -1, cr *= -1;
        if (cl < 0) {
          cl += n;
        }
        if (cr < 0) {
          cr += n;
        }
        swap(cl, cr);
        if (cl <= cr) {
          add(cl, cr, -b[i].first);
        } else {
          add(cl, n - 1, -b[i].first);
          add(0, cr, -b[i].first);
        }
        mx = to;
      }
    }
    if (mx + 1 < n) {
      long long to = n - 1;
      if (mx + 1 <= to) {
        long long cl = (mx + 1) - i, cr = to - i;
        cl *= -1, cr *= -1;
        if (cl < 0) {
          cl += n;
        }
        if (cr < 0) {
          cr += n;
        }
        swap(cl, cr);
        if (cl <= cr) {
          add(cl, cr, b[i].first);
        } else {
          add(cl, n - 1, b[i].first);
          add(0, cr, b[i].first);
        }
        mx = to;
      }
    }
  }
  swap(a, b);
  long long cur = 0;
  for (long long i = 0; i < n; i++) {
    cur += d[i];
    ans[i] += cur;
  }
  pair<long long, long long> best = {1e18, -1};
  for (long long i = 0; i < n; i++) {
    best = min(best, make_pair(ans[i], i));
  }
  cout << best.first << '\n';
  vector<long long> id(n);
  for (long long i = 0; i < n; i++) {
    id[a[(i + best.second) % n].second - 1] = b[i].second;
  }
  for (auto it : id) {
    cout << it << ' ';
  }
  cout << '\n';
}
