#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  map<int, int> cnt, cost;
  vector<int> a(n);
  for (int x = 0; x < n; x++) cin >> a[x];
  ;
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    int x;
    x = a[i];
    cnt[x]++;
    cost[x] += 0;
    int cst = 1;
    while (x > 0) {
      x >>= 1;
      if (cnt[x] < k) {
        cnt[x]++;
        cost[x] += cst;
      }
      cst++;
    }
  }
  long long ans = 1e18;
  for (auto i : cnt) {
    if (i.second >= k) {
      if (cost[i.first] < ans) ans = cost[i.first];
    }
  }
  cout << ans << endl;
}
void sj26() {}
int main() {
  bool q = 0;
  sj26();
  int t = 1;
  if (q) cin >> t;
  while (t--) {
    solve();
  }
}
