#include <bits/stdc++.h>
using namespace std;
long long int solve() {
  long long int n, k;
  cin >> n >> k;
  vector<long long int> v(n);
  for (auto &i : v) cin >> i;
  vector<vector<long long int>> xnum(200001);
  for (auto x : v) {
    long long int cnt = 0;
    while (x) {
      xnum[x].push_back(cnt);
      cnt++;
      x /= 2;
    }
  }
  long long int ans = INT_MAX;
  for (auto x : xnum) {
    sort(x.begin(), x.end());
    if (x.size() < k) continue;
    ans = min(ans, accumulate(x.begin(), x.begin() + k, 0LL));
  }
  cout << ans << '\n';
  return 0;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long int t = 1;
  while (t--) solve();
  return 0;
}
