#include <bits/stdc++.h>
using namespace std;
int arr[400000];
int initialD[400000];
vector<vector<int> > vecc;
vector<int> toint[400000];
int main() {
  int n, k;
  cin >> n >> k;
  vecc.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    initialD[arr[i]]++;
    int tem = arr[i];
    while (tem) {
      vecc[i].push_back(tem);
      tem /= 2;
    }
  }
  for (int i = 0; i < n; i++) {
    int sez = vecc[i].size();
    for (int j = 0; j < sez; j++) {
      toint[vecc[i][j]].push_back(j);
    }
  }
  long long ans = 1000000000;
  for (int i = 0; i < 200001; i++) {
    sort(toint[i].begin(), toint[i].end());
    int sez = toint[i].size();
    if (sez >= k) {
      long long temp = 0;
      for (int j = 0; j < k; j++) {
        temp += toint[i][j];
      }
      ans = min(ans, temp);
    }
  }
  cout << ans << '\n';
}
