#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc = 1;
  while (tc--) {
    int n, k;
    cin >> n >> k;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) cin >> arr[i];
    vector<vector<int>> vals(2 * 100000 + 1);
    for (int &x : arr) {
      int cnt = 0;
      while (x > 0) {
        vals[x].push_back(cnt);
        x /= 2;
        cnt++;
      }
    }
    int ans = 1e9;
    for (int i = 0; i <= 2 * 100000; i++) {
      if (vals[i].size() < k) continue;
      sort(vals[i].begin(), vals[i].end());
      ans = min(ans, accumulate(vals[i].begin(), vals[i].begin() + k, 0));
    }
    cout << ans << "\n";
  }
}
