#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<vector<int> > possval(200005);
  for (int i = 0; i < n; i++) {
    int temp = a[i], iterator = 0;
    while (temp > 0) {
      possval[temp].push_back(iterator);
      temp /= 2;
      iterator++;
    }
  }
  int answer = 1e9;
  for (int i = 0; i < 200005; i++) {
    sort(possval[i].begin(), possval[i].end());
    if (possval[i].size() < k) continue;
    answer =
        min(answer, accumulate(possval[i].begin(), possval[i].begin() + k, 0));
  }
  cout << answer << endl;
}
