#include <bits/stdc++.h>
using namespace std;
const long long inf = 1ll << 50;
priority_queue<long long, vector<long long>, greater<long long> > unuseda;
priority_queue<long long> usedb;
int n, k;
long long a[500500], b[500500];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%I64d", &b[i]);
  long long st = -inf, en = inf;
  while (st <= en) {
    long long mid = (st + en) / 2;
    while (!unuseda.empty()) unuseda.pop();
    while (!usedb.empty()) usedb.pop();
    usedb.push(-inf);
    long long mnsum = 0;
    int mncnt = 0, mxcnt = 0;
    for (int i = 1; i <= n; i++) {
      unuseda.push(a[i] + mid);
      long long tmp1 = unuseda.top() + b[i];
      long long tmp2 = b[i] - usedb.top();
      if (tmp1 >= 0 && tmp2 >= 0) continue;
      if (tmp1 < tmp2) {
        mnsum += tmp1;
        mncnt++;
        unuseda.pop();
        usedb.push(b[i]);
      } else {
        mnsum += tmp2;
        usedb.pop();
        usedb.push(b[i]);
      }
    }
    while (!unuseda.empty()) unuseda.pop();
    while (!usedb.empty()) usedb.pop();
    usedb.push(-inf);
    for (int i = 1; i <= n; i++) {
      unuseda.push(a[i] + mid);
      long long tmp1 = unuseda.top() + b[i];
      long long tmp2 = b[i] - usedb.top();
      if (tmp1 > 0 && tmp2 > 0) continue;
      if (tmp1 <= tmp2) {
        mxcnt++;
        unuseda.pop();
        usedb.push(b[i]);
      } else {
        usedb.pop();
        usedb.push(b[i]);
      }
    }
    if (mncnt <= k && k <= mxcnt) return printf("%I64d\n", mnsum - k * mid), 0;
    if (mxcnt < k)
      en = mid - 1;
    else
      st = mid + 1;
  }
  return 0;
}
