#include <bits/stdc++.h>
using namespace std;
int cnt[200010];
int num[200010];
int a[60];
int main() {
  int n, k;
  while (~scanf("%d%d", &n, &k)) {
    memset(a, 0, sizeof(a));
    memset(num, 0, sizeof(num));
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    sort(a, a + n);
    int ans = 0x3f3f3f3f;
    for (int i = 0; i < n; i++) {
      int x = a[i];
      int d = 0;
      while (x) {
        num[x]++;
        cnt[x] += d;
        if (num[x] == k) {
          ans = min(ans, cnt[x]);
        }
        x /= 2;
        d++;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
