#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, x, co;
  cin >> n >> k;
  vector<pair<int, int>> li(200001, make_pair(0, 0));
  vector<int> da(n);
  for (int i = (0); i < (int)(n); i++) {
    cin >> da[i];
    li[da[i]].first++;
    if (li[da[i]].first > k - 1) {
      cout << 0 << endl;
      return 0;
    }
  }
  bool ju = true;
  co = 0;
  int ans = INT_MAX;
  while (ju) {
    co++;
    ju = false;
    for (int i = (0); i < (int)(n); i++) {
      if (da[i]) {
        da[i] /= 2;
        li[da[i]].first++;
        li[da[i]].second += co;
        ju = true;
        if (li[da[i]].first > k - 1) {
          ans = min(ans, li[da[i]].second);
        }
      }
    }
  }
  cout << ans << endl;
}
