#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const int MOD = 1000000007;
const int nmax = 201234;
int n, k, a[nmax], cnt[2][nmax], res;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  sort(a, a + n);
  for (int i = 0; i < n; ++i) {
    int b = a[i];
    int cnt2 = 0;
    while (b) {
      if (cnt[0][b] < k) {
        cnt[0][b]++;
        cnt[1][b] += cnt2;
      }
      b >>= 1;
      cnt2++;
    }
  }
  res = INF;
  for (int i = 0; i <= 200000; i++)
    if (cnt[0][i] == k && cnt[1][i] < res) res = cnt[1][i];
  printf("%d\n", res);
  return 0;
}
