#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 14;
int n, k;
vector<int> costs[maxn];
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  while (n--) {
    int x;
    cin >> x;
    for (int c = 0;; c++, x >>= 1) {
      costs[x].push_back(c);
      if (x == 0) break;
    }
  }
  int mn = 1e9;
  for (int i = 0; i < maxn; i++)
    if (costs[i].size() >= k) {
      sort(costs[i].begin(), costs[i].end());
      int me = accumulate(costs[i].begin(), costs[i].begin() + k, 0);
      mn = min(mn, me);
    }
  cout << mn << '\n';
}
