#include <bits/stdc++.h>
using namespace std;
struct ext {
  pair<int, pair<int, int> > soja, rev, ulta;
  int mn;
  pair<int, int> ma, mb, tpa, tpb;
};
pair<int, pair<int, int> > bigi = make_pair(INT_MAX, make_pair(0, 0));
int lazy[500005 * 4];
ext tree[500005 * 4];
int ara[500005], ara2[500005];
ext unit(ext a, ext b) {
  ext ret;
  ret.soja = min(a.soja, b.soja);
  ret.soja = min(ret.soja, make_pair(a.ma.first + b.mb.first,
                                     make_pair(a.ma.second, b.mb.second)));
  ret.ulta = min(a.ulta, b.ulta);
  ret.ulta = min(ret.ulta, make_pair(a.mb.first + b.ma.first,
                                     make_pair(b.ma.second, a.mb.second)));
  ret.rev = min(a.rev, b.rev);
  ret.mn = min(a.mn, b.mn);
  ret.ma = min(a.ma, b.ma);
  ret.mb = min(a.mb, b.mb);
  if (a.mn > b.mn) {
    ret.rev = min(ret.rev, a.ulta);
    ret.rev = min(ret.rev, make_pair(a.mb.first + b.tpa.first,
                                     make_pair(b.tpa.second, a.mb.second)));
    ret.tpb = b.tpb;
    ret.tpa = min(a.ma, b.tpa);
  } else if (a.mn < b.mn) {
    ret.rev = min(ret.rev, b.ulta);
    ret.rev = min(ret.rev, make_pair(a.tpb.first + b.ma.first,
                                     make_pair(b.ma.second, a.tpb.second)));
    ret.tpa = a.tpa;
    ret.tpb = min(a.tpb, b.mb);
  } else {
    ret.rev = min(ret.rev, make_pair(a.tpb.first + b.tpa.first,
                                     make_pair(b.tpa.second, a.tpb.second)));
    ret.tpa = a.tpa;
    ret.tpb = b.tpb;
  }
  return ret;
}
void update(int node, int st, int en, int l) {
  if (lazy[node]) {
    tree[node].mn += lazy[node];
    if (st != en) {
      lazy[node * 2] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
  if (st > l or en < l) return;
  if (st == en) {
    ext ret;
    ret.soja = make_pair(ara[st] + ara2[st], make_pair(st, st));
    ret.ulta = make_pair(ara[st] + ara2[st], make_pair(st, st));
    ret.rev = bigi;
    ret.mn = tree[node].mn;
    ret.tpa = ret.ma = make_pair(ara[st], st);
    ret.mb = make_pair(ara2[st], st);
    ret.tpb = make_pair(1e9, 0);
    tree[node] = ret;
    return;
  }
  int mid = (st + en) >> 1;
  update(node * 2, st, mid, l);
  update(node * 2 + 1, mid + 1, en, l);
  tree[node] = unit(tree[node * 2], tree[node * 2 + 1]);
}
void update2(int node, int st, int en, int l, int r, int v) {
  if (l > r) return;
  if (lazy[node]) {
    tree[node].mn += lazy[node];
    if (st != en) {
      lazy[node * 2] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
  if (st > r or en < l) return;
  if (st >= l and en <= r) {
    lazy[node] = v;
    tree[node].mn += lazy[node];
    if (st != en) {
      lazy[node * 2] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
    }
    lazy[node] = 0;
    return;
  }
  int mid = (st + en) >> 1;
  update2(node * 2, st, mid, l, r, v);
  update2(node * 2 + 1, mid + 1, en, l, r, v);
  tree[node] = unit(tree[node * 2], tree[node * 2 + 1]);
}
int main() {
  int i, j, k, l, m, n;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ara[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ara2[i]);
  }
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, i);
  }
  long long ret = 0;
  while (k--) {
    pair<int, pair<int, int> > ps = min(tree[1].soja, tree[1].rev);
    ret += ps.first;
    int e = ps.second.first, f = ps.second.second;
    int g = 1;
    if (ps.second.first > ps.second.second) {
      g = -1;
      swap(ps.second.first, ps.second.second);
    }
    update2(1, 1, n, ps.second.first, ps.second.second - 1, g);
    ara[e] = 1e9;
    ara2[f] = 1e9;
    update(1, 1, n, e);
    update(1, 1, n, f);
  }
  cout << ret << endl;
}
