#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e13;
priority_queue<long long, vector<long long>, greater<long long> > qa;
priority_queue<long long> qb;
long long a[500010], b[500010];
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long l = 0, r = inf, ans = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    long long tmp = 0;
    int siz = 0;
    while (!qa.empty()) qa.pop();
    while (!qb.empty()) qb.pop();
    for (int i = 1; i <= n; i++) {
      qa.push(a[i]);
      long long tmp1 = qa.top() + b[i] - mid;
      long long tmp2 = qb.empty() ? inf : b[i] - qb.top();
      if (tmp1 <= tmp2 && tmp1 <= 0) {
        tmp += tmp1;
        siz++;
        qb.push(b[i]);
        qa.pop();
      } else if (tmp2 < tmp1 && tmp2 <= 0) {
        tmp += tmp2;
        qb.pop();
        qb.push(b[i]);
      }
    }
    if (siz >= m) {
      ans = tmp + mid * m;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
