#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];
  std::unordered_map<string, int> mc;
  std::unordered_map<string, int> mx[35];
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
  }
  for (int i = (0); i < (n); i++) {
    int x = a[i];
    for (int j = (0); j < (100); j++) {
      string bin = std::bitset<22>(x).to_string();
      mc[bin] = mc[bin] + 1;
      mx[j][bin] = mx[j][bin] + 1;
      if (x == 0) break;
      x = x / 2;
    }
  }
  int ans = INT_MAX;
  for (auto i : mc) {
    string s = i.first;
    int y = i.second;
    if (y >= k) {
      int ops = 0, ct = 0;
      for (int j = (0); j < (33); j++) {
        ct += mx[j][s];
        ops += mx[j][s] * j;
        if (ct >= k) {
          ops -= j * (ct - k);
          ans = min(ans, ops);
          break;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
