#include <bits/stdc++.h>
using namespace std;
const int maxn = 50 + 5;
const int maxm = 2e5 + 5;
int n, k;
int a[maxm];
vector<int> res[maxm];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) {
    int tmp = a[i], j = 0;
    while (true) {
      res[tmp].push_back(j);
      if (!tmp) break;
      tmp /= 2;
      j++;
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < maxm; ++i) {
    if (res[i].size() < k) continue;
    sort(res[i].begin(), res[i].end());
    int tmp = 0;
    for (int j = 0; j < k; ++j) tmp += res[i][j];
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}
