#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
map<int, int> h, numb;
int n, a[maxn], k;
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    h[a[i]]++;
    if (h[a[i]] == k) return cout << "0\n", 0;
  }
  sort(a, a + n);
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < n; i++) {
    int x = a[i];
    int op = 0;
    while (x > 0) {
      x >>= 1;
      op++;
      h[x]++;
      numb[x] += op;
      if (h[x] == k) {
        ans = min(ans, numb[x]);
      }
    }
    if (h[0] == k) {
      ans = min(ans, numb[0]);
    }
  }
  cout << ans << '\n';
}
