#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000000000001;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  std::vector<int> v[200010];
  for (int i = 0; i < n; i++) {
    int temp = a[i];
    int cnt = 0;
    while (temp) {
      v[temp].push_back(cnt);
      temp /= 2;
      cnt++;
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < 200001; i++) {
    sort(v[i].begin(), v[i].end());
    if (v[i].size() >= k) {
      ans = min(ans, accumulate(v[i].begin(), v[i].begin() + k, 0));
    }
  }
  cout << ans << endl;
}
