#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  static char c;
  for (c = getchar(); !('0' <= c && c <= '9'); c = getchar())
    ;
  for (x = 0; ('0' <= c && c <= '9'); c = getchar()) x = x * 10 + c - 48;
}
inline void read(long long &x) {
  static char c;
  for (c = getchar(); !('0' <= c && c <= '9'); c = getchar())
    ;
  for (x = 0; ('0' <= c && c <= '9'); c = getchar()) x = x * 10 + c - 48;
}
void write(long long x) {
  if (x < 10) {
    putchar('0' + x);
    return;
  }
  write(x / 10);
  putchar('0' + x % 10);
}
inline void writeln(long long x) {
  if (x < 0) putchar('-'), x *= -1;
  write(x);
  putchar('\n');
}
inline void writel(long long x) {
  if (x < 0) putchar('-'), x *= -1;
  write(x);
  putchar(' ');
}
struct Node {
  long long a[500007], tot;
  void push(long long x) {
    a[++tot] = x;
    push_heap(a + 1, a + tot + 1);
  }
  long long top() { return a[1]; }
  void pop() {
    pop_heap(a + 1, a + tot + 1);
    tot--;
  }
  void clear() { tot = 0; }
} pa, pb;
int n, k, v;
long long a[500007], b[500007], l, r, ans, res, ga, gb, Mid;
signed main() {
  read(n);
  read(k);
  for (int i = 1; i <= n; i++) read(a[i]);
  for (int j = 1; j <= n; j++) read(b[j]);
  l = 0;
  r = ((long long)1e18);
  res = ((long long)1e18);
  while (l < r) {
    ans = 0;
    v = 0;
    Mid = l + r >> 1;
    pa.clear();
    pb.clear();
    for (int i = 1; i <= n; i++) {
      pa.push(-a[i]);
      ga = -pa.top() + b[i] - Mid;
      gb = pb.tot ? b[i] - pb.top() : ((long long)1e18);
      if (ga <= gb && ga <= 0) {
        ans += ga;
        v++;
        pa.pop();
        pb.push(b[i]);
      } else if (ga > gb && gb < 0) {
        ans += gb;
        pb.pop();
        pb.push(b[i]);
      }
    }
    if (v >= k) {
      res = ans + Mid * k;
      r = Mid - 1;
    } else
      l = Mid + 1;
  }
  writeln(res);
  return 0;
}
