#include <bits/stdc++.h>
using namespace std;
const long long inf = (1e15);
long long A[500010], B[500010], n, a[500010], b[500010];
pair<long long, int> shit(long long baki) {
  priority_queue<long long, vector<long long>, greater<long long> > sh;
  priority_queue<long long> pq;
  long long ans = 0, cnt = 0;
  for (int i = 0; i <= n - 1; i++) {
    sh.push(A[i]);
    long long c1 = sh.top() + B[i];
    long long c2 = pq.empty() ? -(1e15) : pq.top();
    c2 = B[i] - c2;
    if (c1 > 0 && c2 > 0)
      continue;
    else if (c1 <= c2) {
      sh.pop();
      cnt++;
      ans += c1;
    } else {
      pq.pop();
      ans += c2;
    }
    pq.push(B[i]);
  }
  return {ans, cnt};
}
int main() {
  ios::sync_with_stdio(false);
  long long k;
  cin >> n >> k;
  for (int i = 0; i <= n - 1; i++) cin >> a[i];
  for (int i = 0; i <= n - 1; i++) cin >> b[i];
  long long st = -inf, en = inf, mid, val = -inf, can = 10;
  while (st <= en) {
    mid = (long long)(st + en) >> 1;
    for (int i = 0; i <= n - 1; i++) A[i] = a[i] + mid;
    for (int i = 0; i <= n - 1; i++) B[i] = b[i] + mid;
    if (shit(mid).second >= k)
      st = mid + 1, val = max(val, mid);
    else
      en = mid - 1;
  }
  for (int i = 0; i <= n - 1; i++) A[i] = a[i] + val;
  for (int i = 0; i <= n - 1; i++) B[i] = b[i] + val;
  cout << shit(val).first + (shit(val).second * -val * 2) << endl;
  return 0;
}
