#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int w = 0, x = 0;
  char c = getchar();
  while (!isdigit(c)) w |= c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  return w ? -x : x;
}
namespace star {
const int maxn = 5e5 + 10, INF = 0x3f3f3f3f;
int n, k, a[maxn], b[maxn], aa[maxn << 2], ab[maxn << 2], ba[maxn << 2],
    bb[maxn << 2], vm[maxn << 2], tag[maxn << 2];
long long ans;
struct path {
  int x, y;
  path() {}
  path(int x, int y) : x(x), y(y) {}
  path operator+(const path &zp) const {
    return a[x] + b[y] < a[zp.x] + b[zp.y] ? *this : zp;
  }
} va[maxn << 2], vb[maxn << 2], vc[maxn << 2];
inline void add(int x, int a) { vm[x] += a, tag[x] += a; }
inline void pushdown(const int &ro) {
  if (tag[ro])
    add((ro << 1), tag[ro]), add((ro << 1 | 1), tag[ro]), tag[ro] = 0;
}
inline void pushup(const int &ro) {
  va[ro] = va[(ro << 1)] + va[(ro << 1 | 1)] +
           path(aa[(ro << 1)], ab[(ro << 1 | 1)]);
  vc[ro] = vc[(ro << 1)] + vc[(ro << 1 | 1)] +
           path(aa[(ro << 1 | 1)], ab[(ro << 1)]);
  aa[ro] = a[aa[(ro << 1)]] < a[aa[(ro << 1 | 1)]] ? aa[(ro << 1)]
                                                   : aa[(ro << 1 | 1)];
  ab[ro] = b[ab[(ro << 1)]] < b[ab[(ro << 1 | 1)]] ? ab[(ro << 1)]
                                                   : ab[(ro << 1 | 1)];
  if (vm[(ro << 1)] < vm[(ro << 1 | 1)]) {
    vb[ro] = vb[(ro << 1)] + vb[(ro << 1 | 1)] + vc[(ro << 1 | 1)] +
             path(aa[(ro << 1 | 1)], bb[(ro << 1)]);
    ba[ro] = ba[(ro << 1)];
    bb[ro] = b[ab[(ro << 1 | 1)]] < b[bb[(ro << 1)]] ? ab[(ro << 1 | 1)]
                                                     : bb[(ro << 1)];
    vm[ro] = vm[(ro << 1)];
  } else if (vm[(ro << 1)] > vm[(ro << 1 | 1)]) {
    vb[ro] = vb[(ro << 1)] + vb[(ro << 1 | 1)] + vc[(ro << 1)] +
             path(ba[(ro << 1 | 1)], ab[(ro << 1)]);
    ba[ro] = a[aa[(ro << 1)]] < a[ba[(ro << 1 | 1)]] ? aa[(ro << 1)]
                                                     : ba[(ro << 1 | 1)];
    bb[ro] = bb[(ro << 1 | 1)];
    vm[ro] = vm[(ro << 1 | 1)];
  } else {
    vb[ro] = vb[(ro << 1)] + vb[(ro << 1 | 1)] +
             path(ba[(ro << 1 | 1)], bb[(ro << 1)]);
    ba[ro] = ba[(ro << 1)];
    bb[ro] = bb[(ro << 1 | 1)];
    vm[ro] = vm[(ro << 1)];
  }
}
void build(const int &ro = 1, const int &l = 0, const int &r = n) {
  if (l == r)
    return va[ro] = vc[ro] = path(l, l), aa[ro] = ab[ro] = ba[ro] = l, void();
  build((ro << 1), l, ((l + r) >> 1)),
      build((ro << 1 | 1), ((l + r) >> 1) + 1, r);
  pushup(ro);
}
void add(const int &x, const int &y, const int &k, const int &ro = 1,
         const int &l = 0, const int &r = n) {
  if (x == l and y == r) return add(ro, k);
  pushdown(ro);
  if (y <= ((l + r) >> 1))
    add(x, y, k, (ro << 1), l, ((l + r) >> 1));
  else if (x > ((l + r) >> 1))
    add(x, y, k, (ro << 1 | 1), ((l + r) >> 1) + 1, r);
  else
    add(x, ((l + r) >> 1), k, (ro << 1), l, ((l + r) >> 1)),
        add(((l + r) >> 1) + 1, y, k, (ro << 1 | 1), ((l + r) >> 1) + 1, r);
  pushup(ro);
}
void update(const int &x, const int &ro = 1, const int &l = 0,
            const int &r = n) {
  if (l == r) return;
  pushdown(ro);
  x <= ((l + r) >> 1) ? update(x, (ro << 1), l, ((l + r) >> 1))
                      : update(x, (ro << 1 | 1), ((l + r) >> 1) + 1, r);
  pushup(ro);
}
inline void work() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  a[0] = b[0] = INF;
  build();
  while (k--) {
    path res = va[1] + vb[1];
    int x = res.x, y = res.y;
    ans += a[x] + b[y];
    if (x < y) add(x, y - 1, 1);
    if (y < x) add(y, x - 1, -1);
    a[x] = INF, update(x), b[y] = INF, update(y);
  }
  printf("%lld\n", ans);
}
}  // namespace star
signed main() {
  star::work();
  return 0;
}
