#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200005];
vector<int> v[200005];
int main() {
  cin >> n >> k;
  int ans = 9999999;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int cnt = 0;
    if (a[i] == 0) v[a[i]].push_back(cnt);
    while (a[i]) {
      v[a[i]].push_back(cnt);
      a[i] /= 2;
      cnt++;
    }
  }
  for (int p = 0; p <= 200000; p++) {
    if (v[p].size() < k) continue;
    sort(v[p].begin(), v[p].end());
    int sum = 0;
    for (int i = 0; i < k; i++) sum += v[p][i];
    ans = min(sum, ans);
  }
  cout << ans;
  return 0;
}
