#include <bits/stdc++.h>
using namespace std;
int n, k;
int dem[200005];
int a[200006];
int kq[200006];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int buoc;
  int rs = 1000000000;
  for (int i = 0; i < n; i++) {
    buoc = 0;
    while (a[i] > 0) {
      dem[a[i]] += 1;
      kq[a[i]] += buoc;
      if (dem[a[i]] >= k) {
        rs = min(rs, kq[a[i]]);
      }
      buoc += 1;
      a[i] /= 2;
    }
  }
  cout << rs;
}
