#include <bits/stdc++.h>
inline int Get() {
  char ch;
  int Num, Sign = 0;
  while ((ch = getchar()) < '0' || ch > '9')
    if (ch == '-') Sign ^= 1;
  Num = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    Num = (Num << 3) + (Num << 1) + ch - '0';
  return Sign ? -Num : Num;
}
const int N = 5e5 + 5;
const int inf = 1e9;
int n, m, a[N], b[N];
int mn[N << 2], tag[N << 2];
std ::pair<int, int> mnA[N << 2], mnB[N << 2], vA[N << 2], vB[N << 2];
std ::pair<int, std ::pair<int, int> > p0[N << 2], p1[N << 2], v1[N << 2];
void down(int k) {
  if (tag[k]) {
    mn[k << 1] += tag[k], mn[k << 1 | 1] += tag[k];
    tag[k << 1] += tag[k], tag[k << 1 | 1] += tag[k];
    tag[k] = 0;
  }
}
void up(int k) {
  int l = k << 1, r = k << 1 | 1;
  p0[k] =
      std ::min(std ::min(p0[l], p0[r]),
                std ::make_pair(mnA[l].first + mnB[r].first,
                                std ::make_pair(mnA[l].second, mnB[r].second)));
  p1[k] =
      std ::min(std ::min(p1[l], p1[r]),
                std ::make_pair(mnA[r].first + mnB[l].first,
                                std ::make_pair(mnA[r].second, mnB[l].second)));
  v1[k] = std ::min(v1[l], v1[r]);
  mnA[k] = std ::min(mnA[l], mnA[r]), mnB[k] = std ::min(mnB[l], mnB[r]);
  if (mn[l] < mn[r]) {
    mn[k] = mn[l];
    v1[k] = std ::min(
        std ::min(v1[k], p1[r]),
        std ::make_pair(mnA[r].first + vB[l].first,
                        std ::make_pair(mnA[r].second, vB[l].second)));
    vA[k] = vA[l], vB[k] = std ::min(vB[l], mnB[r]);
  }
  if (mn[l] > mn[r]) {
    mn[k] = mn[r];
    v1[k] = std ::min(
        std ::min(v1[k], p1[l]),
        std ::make_pair(vA[r].first + mnB[l].first,
                        std ::make_pair(vA[r].second, mnB[l].second)));
    vA[k] = std ::min(mnA[l], vA[r]), vB[k] = vB[r];
  }
  if (mn[l] == mn[r]) {
    mn[k] = mn[l];
    v1[k] = std ::min(
        v1[k], std ::make_pair(vA[r].first + vB[l].first,
                               std ::make_pair(vA[r].second, vB[l].second)));
    vA[k] = vA[l], vB[k] = vB[r];
  }
}
void build(int k, int l, int r) {
  if (l == r) {
    p0[k] = p1[k] = std ::make_pair(a[l] + b[l], std ::make_pair(l, l));
    v1[k] = std ::make_pair(inf * 2, std ::make_pair(0, 0));
    vA[k] = std ::make_pair(a[l], l), vB[k] = std ::make_pair(inf, 0);
    mnA[k] = std ::make_pair(a[l], l), mnB[k] = std ::make_pair(b[l], l);
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
  up(k);
}
void modify0(int k, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    mn[k] += val, tag[k] += val;
    return;
  }
  int mid = l + r >> 1;
  down(k);
  if (x <= mid) modify0(k << 1, l, mid, x, y, val);
  if (y > mid) modify0(k << 1 | 1, mid + 1, r, x, y, val);
  up(k);
  return;
}
void modify1(int k, int l, int r, int pos) {
  if (l == r) {
    p0[k] = p1[k] = std ::make_pair(a[l] + b[l], std ::make_pair(l, l));
    v1[k] = std ::make_pair(inf * 2, std ::make_pair(0, 0));
    vA[k] = std ::make_pair(a[l], l), vB[k] = std ::make_pair(inf, 0);
    mnA[k] = std ::make_pair(a[l], l), mnB[k] = std ::make_pair(b[l], l);
    return;
  }
  int mid = l + r >> 1;
  down(k);
  if (pos <= mid)
    modify1(k << 1, l, mid, pos);
  else
    modify1(k << 1 | 1, mid + 1, r, pos);
  up(k);
  return;
}
int main() {
  n = Get(), m = Get();
  a[0] = inf;
  for (int i = 1; i <= n; ++i) a[i] = Get();
  b[0] = inf;
  for (int i = 1; i <= n; ++i) b[i] = Get();
  build(1, 0, n);
  long long ans = 0;
  while (m--) {
    std ::pair<int, std ::pair<int, int> > res = std ::min(p0[1], v1[1]);
    ans += res.first;
    if (res.second.first < res.second.second)
      modify0(1, 0, n, res.second.first, res.second.second - 1, 1);
    if (res.second.second < res.second.first)
      modify0(1, 0, n, res.second.second, res.second.first - 1, -1);
    a[res.second.first] = inf, modify1(1, 0, n, res.second.first);
    b[res.second.second] = inf, modify1(1, 0, n, res.second.second);
  }
  printf("%I64d\n", ans);
}
