#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn];
int b[maxn];
int tmp[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i], b[a[i]]++;
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= 2e5; i++) {
    if (b[i] >= k) {
      ans = 0;
    }
    int tp = i;
    int cnt = 1;
    int sum = b[i];
    while (tp) {
      if (b[tp / 2] + sum >= k) {
        ans = max(0, min(ans, (k - b[tp / 2]) * cnt + tmp[tp / 2]));
      } else {
        b[tp / 2] += sum;
        tmp[tp / 2] += cnt++ * sum;
      }
      tp /= 2;
    }
  }
  cout << ans << endl;
  return 0;
}
