#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
int a[200005] = {0}, ci[200005] = {0}, now[200005] = {0};
int main() {
  int n, kk;
  scanf("%d", &n), scanf("%d", &kk);
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    now[a[i]]++;
    if (now[a[i]] >= kk) ans = 0;
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    int temp = a[i];
    int k = 1;
    while (temp != 0) {
      temp /= 2;
      ci[temp] += k;
      now[temp]++;
      if (now[temp] >= kk) ans = min(ci[temp], ans);
      k++;
    }
  }
  printf("%d", ans);
}
