#include <bits/stdc++.h>
using namespace std;
int n, k, s, t, tot = 2147483647, a[200005], b[200005], c[200005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    s = a[i];
    t = 0;
    while (s) {
      b[s]++;
      c[s] += t;
      if (b[s] == k && c[s] < tot) tot = c[s];
      s /= 2;
      t++;
    }
  }
  cout << tot << endl;
  return 0;
}
