#include <bits/stdc++.h>
using namespace std;
long long int t, n, i, j;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int k;
  cin >> n >> k;
  vector<long long int> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  long long int ans = 1e18;
  long long int cnt[200005] = {0};
  long long int cst[200005] = {0};
  sort(a.begin(), a.end());
  for (i = 0; i < n; i++) {
    cnt[a[i]]++;
    if (cnt[a[i]] == k) ans = min(ans, cst[a[i]]);
    j = 0;
    while (a[i] > 0) {
      a[i] = a[i] / 2;
      j++;
      cnt[a[i]]++;
      if (cnt[a[i]] <= k) cst[a[i]] += j;
      if (cnt[a[i]] == k) ans = min(ans, cst[a[i]]);
    }
  }
  cout << ans << endl;
}
