#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  map<int, int> mp;
  int flag = 0;
  for (int i = 0; i < n; i++) {
    mp[arr[i]]++;
    if (mp[arr[i]] >= k) {
      flag = 1;
    }
  }
  if (flag)
    cout << 0 << endl;
  else {
    vector<int> vect[arr[n - 1] + 1];
    int upto = arr[n - 1];
    for (int i = 0; i < n; i++) {
      int numOfOpe = 0;
      while (arr[i]) {
        vect[arr[i]].push_back(numOfOpe);
        arr[i] = arr[i] / 2;
        numOfOpe++;
      }
      vect[0].push_back(numOfOpe);
    }
    int ans = INT_MAX, curr_ans = 0;
    for (int i = 0; i <= upto; i++) {
      if (vect[i].size() >= k) {
        curr_ans = 0;
        for (int j = 0; j < k; j++) {
          curr_ans += vect[i][j];
        }
        ans = min(ans, curr_ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
