#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, ans = 1000000000;
  cin >> n >> k;
  vector<vector<int>> cnt(200001);
  for (int i = 0; i < n; ++i) {
    int a, c = 0;
    cin >> a;
    for (; a > 0; a >>= 1, ++c) cnt[a].emplace_back(c);
    cnt[a].emplace_back(c);
  }
  for (int i = 0; i <= 200000; ++i)
    if (cnt[i].size() >= k) {
      sort(cnt[i].begin(), cnt[i].end());
      int sum = 0;
      for (int j = 0; j < k; ++j) sum += cnt[i][j];
      ans = min(ans, sum);
    }
  cout << ans;
  return 0;
}
