#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i, j, count = INT_MAX;
  cin >> n >> k;
  vector<int> A(n, 0);
  map<int, int> M, N;
  for (i = 0; i < n; i++) cin >> A[i];
  sort(A.begin(), A.end());
  for (i = 0; i < n; i++) {
    j = 0;
    while (A[i] != 0) {
      if (M.find(A[i]) == M.end()) {
        M[A[i]] = 1;
        N[A[i]] = j;
      } else if (M[A[i]] < k) {
        M[A[i]]++;
        N[A[i]] += j;
      } else
        break;
      A[i] = A[i] / 2;
      j++;
    }
  }
  map<int, int>::iterator it;
  for (it = M.begin(); it != M.end(); it++) {
    if (it->second < k) continue;
    count = min(count, N[it->first]);
  }
  cout << count << endl;
  return 0;
}
