#include <bits/stdc++.h>
int comp(const void *p, const void *q) {
  return *(const int *)p - *(const int *)q;
}
int main() {
  long long n, k;
  scanf("%lld%lld", &n, &k);
  long long arr[n], count = 0, x, y, flag = 0, run = 0, min = 200000001;
  long long num[n][20], cnt[200001] = {0};
  for (int i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
  }
  qsort(arr, n, sizeof(arr[0]), comp);
  for (int i = 0; i < n; i++) {
    num[i][0] = arr[i];
    cnt[num[i][0]]++;
    for (int j = 1; j < 20; j++) {
      num[i][j] = num[i][j - 1] / 2;
      cnt[num[i][j]]++;
    }
  }
  for (int z = 200000; z >= 0; z--) {
    count = 0;
    run = 0;
    flag = 0;
    if (cnt[z] >= k) {
      x = z;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < 20; j++) {
          if (num[i][j] == x) {
            run++;
            count = count + j;
            if (run >= k) {
              flag = 1;
              break;
            }
            break;
          }
        }
        if (flag == 1) break;
      }
      if (count < min) {
        min = count;
      }
    }
  }
  printf("%lld", min);
  return 0;
}
