#include <bits/stdc++.h>
using namespace std;
int arr[150000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<vector<int>> vals(200 * 1000);
  for (int i = 0; i < n; i++) {
    int x = v[i];
    int cur = 0;
    while (x > 0) {
      vals[x].push_back(cur);
      x /= 2;
      ++cur;
    }
  }
  int ans = 1e9;
  for (int i = 0; i <= 200 * 1000; i++) {
    sort(vals[i].begin(), vals[i].end());
    if (int(vals[i].size()) < k) continue;
    ans = min(ans, accumulate(vals[i].begin(), vals[i].begin() + k, 0));
  }
  cout << ans;
  return 0;
}
