#include <bits/stdc++.h>
using namespace std;
struct str {
  priority_queue<int, vector<int>, greater<int> > pq;
} info[200001];
int n, k, i, cnt, ans = INT_MAX, ans2, in2, a[200001];
int main() {
  cin >> n >> k;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, greater<int>());
  for (i = 1; i <= n; i++) {
    cnt = 0;
    in2 = a[i];
    while (in2 > 0) {
      info[in2].pq.push(cnt);
      cnt++;
      in2 >>= 1;
    }
  }
  for (i = 1; i < 200001; i++) {
    ans2 = cnt = 0;
    if (info[i].pq.size() >= k) {
      while (!info[i].pq.empty()) {
        in2 = info[i].pq.top();
        info[i].pq.pop();
        ans2 += in2;
        cnt++;
        if (cnt == k) break;
      }
      ans = min(ans, ans2);
    }
  }
  printf("%d\n", ans);
}
