#include <bits/stdc++.h>
using namespace std;
int read() {
  char c = getchar();
  int x = 0;
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
  return x;
}
const int _ = 5e5 + 7;
const int M = _ << 2;
int A[M], B[M];
int MinL(int x, int y) {
  if (!x || !y) return x | y;
  return (A[x] < A[y]) ? x : y;
}
int MinR(int x, int y) {
  if (!x || !y) return x | y;
  return (B[x] < B[y]) ? x : y;
}
pair<int, int> Min(pair<int, int> a, pair<int, int> b) {
  long long v1 = A[a.first] + B[a.second];
  long long v2 = A[b.first] + B[b.second];
  return (v1 < v2) ? a : b;
}
namespace Seg {
int lc[M], rc[M], lmn[M], rmn[M];
int lx[M], rx[M];
int mn[M];
int tag[M];
pair<int, int> ans1[M], ans2[M], ans3[M];
int cnt = 0;
void pushup(int p) {
  int ls = lc[p], rs = rc[p];
  lmn[p] = MinL(lmn[ls], lmn[rs]);
  rmn[p] = MinR(rmn[ls], rmn[rs]);
  ans1[p] = Min(make_pair(lmn[ls], rmn[rs]), Min(ans1[ls], ans1[rs]));
  ans3[p] = Min(make_pair(lmn[rs], rmn[ls]), Min(ans3[ls], ans3[rs]));
  mn[p] = min(mn[ls], mn[rs]);
  ans2[p] = Min(ans2[ls], ans2[rs]);
  int pr = rx[ls], pl = lx[rs];
  if (mn[ls] > mn[p]) pr = MinR(pr, rmn[ls]), ans2[p] = Min(ans2[p], ans3[ls]);
  if (mn[rs] > mn[p]) pl = MinL(pl, lmn[rs]), ans2[p] = Min(ans2[p], ans3[rs]);
  ans2[p] = Min(make_pair(pl, pr), ans2[p]);
  lx[p] = lx[ls];
  if (mn[ls] > mn[p]) lx[p] = MinL(lx[p], lx[rs]), lx[p] = MinL(lx[p], lmn[ls]);
  rx[p] = rx[rs];
  if (mn[rs] > mn[p]) rx[p] = MinR(rx[p], rx[ls]), rx[p] = MinR(rx[p], rmn[rs]);
}
void pushadd(int p, int v) {
  mn[p] += v;
  tag[p] += v;
}
void pushdown(int p) {
  if (tag[p]) {
    if (lc[p]) pushadd(lc[p], tag[p]);
    if (rc[p]) pushadd(rc[p], tag[p]);
    tag[p] = 0;
  }
}
void build(int &p, int l, int r) {
  if (!p) p = ++cnt;
  int mid = (l + r) >> 1;
  if (l == r) {
    lmn[p] = l;
    rmn[p] = r;
    lx[p] = l;
    rx[p] = 0;
    ans1[p] = make_pair(l, r);
    ans3[p] = make_pair(l, r);
    return;
  }
  build(lc[p], l, mid);
  build(rc[p], mid + 1, r);
  pushup(p);
}
void modify(int p, int l, int r, int a, int b, int v) {
  if (a > b) return;
  if (a <= l && b >= r) {
    pushadd(p, v);
    return;
  }
  pushdown(p);
  int mid = (l + r) >> 1;
  if (a <= mid) modify(lc[p], l, mid, a, b, v);
  if (b > mid) modify(rc[p], mid + 1, r, a, b, v);
  pushup(p);
}
void update(int p, int l, int r, int pos) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  pushdown(p);
  if (pos <= mid)
    update(lc[p], l, mid, pos);
  else
    update(rc[p], mid + 1, r, pos);
  pushup(p);
}
}  // namespace Seg
using namespace Seg;
int main() {
  A[0] = B[0] = 1e9 + 7;
  int n = read(), k = read();
  for (int i = 1; i <= n; ++i) A[i] = read();
  for (int i = 1; i <= n; ++i) B[i] = read();
  int rt = 0;
  Seg::build(rt, 1, n);
  long long ans = 0;
  for (int i = 1; i <= k; ++i) {
    pair<int, int> now = Min(Seg::ans1[rt], Seg::ans2[rt]);
    int u = now.first, v = now.second;
    pair<int, int> Now = Seg::ans2[rt];
    ans += (1ll * A[u] + B[v]);
    A[u] = 1e9 + 7, B[v] = 1e9 + 7;
    Seg::update(rt, 1, n, u);
    Seg::update(rt, 1, n, v);
    if (u <= v)
      Seg::modify(rt, 1, n, u, v - 1, 1);
    else
      Seg::modify(rt, 1, n, v, u - 1, -1);
  }
  printf("%lld\n", ans);
  return 0;
}
