#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200001];
int vis[200001];
int op[200001];
void print(int a[]) {
  for (int i = 0; i < n; i++) {
    printf("%d ", a[i]);
  }
  printf("\n");
}
int main(int argc, char *argv[]) {
  int res = 0x3f3f3f3f;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    vis[a[i]]++;
    if (vis[a[i]] >= k) {
      printf("0\n");
      return 0;
    }
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    int div = 0;
    while (a[i] != 0) {
      a[i] /= 2;
      div++;
      vis[a[i]]++;
      op[a[i]] += div;
      if (vis[a[i]] == k) {
        res = min(res, op[a[i]]);
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
