#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n, 0);
  int i;
  map<int, int> m1, m2;
  int mn = INT_MAX;
  int mx = 0;
  for (i = 0; i < n; i++) {
    cin >> v[i];
  }
  int s = -1, count = 0;
  sort(v.begin(), v.end());
  for (int j = 0; j < n; j++) {
    while (v[j] >= 0) {
      if (v[j] == 0) {
        count++;
        if (count > 1) break;
      }
      s++;
      m1[v[j]]++;
      m2[v[j]] += s;
      if (m1[v[j]] >= k) {
        if (m2[v[j]] < mn) {
          mn = m2[v[j]];
        }
      }
      v[j] = v[j] / 2;
    }
    s = -1;
    count = 0;
  }
  cout << mn << endl;
  return 0;
}
