#include <bits/stdc++.h>
using namespace std;
int a[1919810], m[1919810], s[1919810], n, k, minn = 1234567890, maxx;
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    maxx = max(maxx, a[i]);
    m[a[i]]++;
    if (m[a[i]] == k) exit(0 & puts("0"));
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) {
    int step = 0;
    while (a[i] != 0) {
      a[i] >>= 1, ++step;
      if (m[a[i]] == k) continue;
      ++m[a[i]], s[a[i]] += step;
    }
    if (a[i] == 0) ++m[0], s[0] += step;
  }
  int ans = 999999999;
  for (int i = 1; i <= maxx; i++)
    if (m[i] >= k) ans = min(ans, s[i]);
  printf("%d", ans);
  return 0;
}
