#include <bits/stdc++.h>
using namespace std;
multiset<int> s[200005];
int main() {
  int n, k, i, x, cf, j, ans = 2000000000, sum;
  scanf("%d%d", &n, &k);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    cf = 0;
    while (x > 0) {
      s[x].insert(cf);
      cf++;
      x /= 2;
    }
    s[x].insert(cf);
  }
  for (i = 0; i <= 200000; i++)
    if (s[i].size() >= k) {
      set<int>::iterator it;
      sum = 0;
      for (it = s[i].begin(), j = 0; j < k; j++, it++) sum += *it;
      ans = min(ans, sum);
    }
  printf("%d", ans);
  return 0;
}
