#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, k, arr[200005];
vector<int> states;
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<vector<int> > sat(200005 + 30);
  for (int i = 0; i < n; i++) {
    int w = arr[i];
    int dev = 0;
    while (w > 0) {
      sat[w].push_back(dev);
      states.push_back(w);
      w /= 2;
      dev++;
    }
  }
  int A = INT_MAX;
  for (vector<int> kaz : sat) {
    if (kaz.size() >= k) {
      sort(kaz.begin(), kaz.end());
      int jemerends = 0;
      for (int i = 0; i < k; i++) {
        jemerends += kaz[i];
      }
      A = min(A, jemerends);
    }
  }
  cout << A << '\n';
}
