#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i, j, nu, dist, min_op, m;
  cin >> n >> k;
  vector<int> arr(n);
  for (i = 0; i < n; i++) cin >> arr[i];
  sort(arr.begin(), arr.end());
  map<int, vector<int> > bucket;
  map<int, vector<int> >::iterator it1, it2;
  for (i = 0; i < n; i++) {
    nu = arr[i];
    dist = 0;
    while (nu > 0) {
      bucket[nu].push_back(dist);
      nu /= 2;
      dist++;
    }
    bucket[0].push_back(dist);
  }
  min_op = 1000000007;
  nu = 0;
  it2 = bucket.end();
  for (it1 = bucket.begin(); it1 != it2; it1++) {
    vector<int> v1 = it1->second;
    if (v1.size() >= k) {
      m = 0;
      for (i = 0; i < k; i++) m += v1[i];
      if (m < min_op) min_op = m;
    }
    v1.clear();
  }
  cout << min_op << endl;
  bucket.clear();
  arr.clear();
  return 0;
}
