#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn];
vector<int> v[maxn];
int main() {
  int n, k, i, j;
  cin >> n >> k;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) {
    int cnt = 0;
    while (a[i] != 0) {
      v[a[i]].push_back(cnt);
      cnt++;
      a[i] /= 2;
    }
  }
  int ans = 1000000;
  for (i = 0; i < maxn; i++) {
    if (v[i].size() < k) continue;
    sort(v[i].begin(), v[i].end());
    int tmp = 0;
    for (j = 0; j < k; j++) tmp += v[i][j];
    ans = min(ans, tmp);
  }
  cout << ans << "\n";
}
