#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, k, ans = N;
vector<int> a[N];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    int x, j = 0;
    scanf("%d", &x);
    while (x) {
      a[x].push_back(j);
      j++;
      x >>= 1;
    }
  }
  for (int i = 0; i <= 200000; ++i)
    if (a[i].size() >= k) {
      int mx = 0;
      sort(a[i].begin(), a[i].end());
      for (int j = 0; j < k; ++j) mx += a[i][j];
      ans = min(ans, mx);
    }
  printf("%d", ans);
}
