#include <bits/stdc++.h>
using namespace std;
long long a[200001], b[200001];
int ta[200001], tb[200001];
vector<int> pxch[200001], pych[200001];
bool fa[200001][5], fb[200001][5];
struct earth {
  int x, p;
};
earth ea[200001], eb[200001];
int sol[200001];
bool cmp(earth a, earth b) { return a.x < b.x; }
int binsh(long long x, long long v[], int n) {
  if (v[n - 1] < x) {
    return -1;
  }
  int st = 0, dr = n - 1, med;
  while (st < dr) {
    med = (st + dr) >> 1;
    if (v[med] < x) {
      st = med + 1;
    } else {
      dr = med;
    }
  }
  return st;
}
int binsl(long long x, long long v[], int n) {
  if (v[0] > x) {
    return -1;
  }
  if (v[n - 1] <= x) {
    return n - 1;
  }
  int st = 0, dr = n - 1, med;
  while (st < dr) {
    med = (st + dr) >> 1;
    if (v[med] > x) {
      dr = med - 1;
    } else {
      st = med;
    }
    if (v[dr] > x) {
      dr--;
    }
    if (v[dr] <= x) {
      return dr;
    }
  }
  return dr;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i, j, minp = 0, val;
  long long mindist = 0, dist, m;
  cin >> m >> n;
  for (i = 0; i < n; ++i) {
    cin >> a[i];
    ea[i].x = a[i];
    ea[i].p = i;
  }
  for (i = 0; i < n; ++i) {
    cin >> b[i];
    eb[i].x = b[i];
    eb[i].p = i;
  }
  sort(ea, ea + n, cmp);
  sort(eb, eb + n, cmp);
  sort(a, a + n);
  sort(b, b + n);
  for (i = 0; i < n; ++i) {
    if ((b[0] << 1) < (a[i] << 1) - m) {
      pxch[n - i].push_back(i);
      fa[i][1] = 1;
    }
    val = binsh(a[i] - (m >> 1), b, n);
    if (val != -1 && b[val] < a[i]) {
      fa[i][2] = 1;
      if (val >= i) {
        pxch[val - i].push_back(i);
      } else {
        pxch[n + val - i].push_back(i);
      }
    }
    val = binsh(a[i], b, n);
    if (val != -1 && (b[val] << 1) < (a[i] << 1) + m) {
      fa[i][3] = 1;
      if (val >= i) {
        pxch[val - i].push_back(i);
      } else {
        pxch[n - i + val].push_back(i);
      }
    }
    long long valts;
    if (m & 1) {
      valts = a[i] + (m >> 1) + 1;
    } else {
      valts = a[i] + (m >> 1);
    }
    val = binsh(valts, b, n);
    if (val != -1) {
      fa[i][4] = 1;
      if (val >= i) {
        pxch[val - i].push_back(i);
      } else {
        pxch[n - i + val].push_back(i);
      }
    }
    if ((a[n - 1] << 1) > (b[i] << 1) + m && (a[0] << 1) <= (b[i] << 1) + m) {
      fb[i][1] = 1;
      pych[i + 1].push_back(i);
    }
    val = binsl(b[i] + (m >> 1), a, n);
    if (val != -1 && b[i] < a[val]) {
      fb[i][2] = 1;
      if (val >= i) {
        pych[n - val + i].push_back(i);
      } else {
        pych[i - val].push_back(i);
      }
    }
    val = binsl(b[i], a, n);
    valts = b[i] - (m >> 1);
    if (m & 1) {
      valts--;
    }
    if (val != -1 && valts < a[val]) {
      fb[i][3] = 1;
      if (val >= i) {
        pych[n - val + i].push_back(i);
      } else {
        pych[i - val].push_back(i);
      }
    }
    valts = b[i] - (m >> 1);
    if (m & 1) {
      valts--;
    }
    val = binsl(valts, a, n);
    if (val != -1) {
      fb[i][4] = 1;
      if (val >= i) {
        pych[n - val + i].push_back(i);
      } else {
        pych[i - val].push_back(i);
      }
    }
    if ((b[i] << 1) < (a[i] << 1) - m) {
      ta[i] = 1;
      tb[i] = 1;
      mindist += (b[i] + m - a[i]);
    } else {
      if (b[i] < a[i]) {
        ta[i] = 2;
        tb[i] = 2;
        mindist += (a[i] - b[i]);
      } else {
        if ((b[i] << 1) < (a[i] << 1) + m) {
          ta[i] = 3;
          tb[i] = 3;
          mindist += (b[i] - a[i]);
        } else {
          ta[i] = 4;
          tb[i] = 4;
          mindist += (a[i] + m - b[i]);
        }
      }
    }
  }
  dist = mindist;
  int pos;
  for (i = 1; i < n; ++i) {
    for (j = 0; j < (int)pxch[i].size(); ++j) {
      pos = pxch[i][j];
      if (ta[pos] % 2) {
        dist += a[pos];
      } else {
        dist -= a[pos];
      }
      if (ta[pos] == 1 || ta[pos] == 4) {
        dist -= m;
      }
      ta[pos] = (ta[pos] % 4) + 1;
      while (!fa[pos][ta[pos]]) {
        ta[pos] = (ta[pos] % 4) + 1;
      }
      if (ta[pos] % 2) {
        dist -= a[pos];
      } else {
        dist += a[pos];
      }
      if (ta[pos] == 1 || ta[pos] == 4) {
        dist += m;
      }
    }
    for (j = 0; j < (int)pych[i].size(); ++j) {
      pos = pych[i][j];
      if (tb[pos] % 2) {
        dist -= b[pos];
      } else {
        dist += b[pos];
      }
      tb[pos] = (tb[pos] % 4) + 1;
      while (!fb[pos][tb[pos]]) {
        tb[pos] = (tb[pos] % 4) + 1;
      }
      if (tb[pos] % 2) {
        dist += b[pos];
      } else {
        dist -= b[pos];
      }
    }
    if (dist < mindist) {
      mindist = dist;
      minp = i;
    }
  }
  cout << mindist << endl;
  for (i = 0; i < n; ++i) {
    sol[ea[i].p] = eb[(i + minp) % n].p + 1;
  }
  for (i = 0; i < n; ++i) {
    if (i) {
      cout << " ";
    }
    cout << sol[i];
  }
  cout << endl;
  return 0;
}
