#include <bits/stdc++.h>
using namespace std;
map<string, vector<int> > mp;
string s[200000 + 10];
int main() {
  int sum = 99999999;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    while (x) {
      s[i] += x % 2;
      x /= 2;
    }
    reverse(s[i].begin(), s[i].end());
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < s[i].size(); j++)
      mp[s[i].substr(0, j + 1)].push_back(s[i].size() - j - 1);
  }
  for (map<string, vector<int> >::iterator it = mp.begin(); it != mp.end();
       it++) {
    vector<int> a = it->second;
    if (a.size() < k) continue;
    sort(a.begin(), a.end());
    int t = 0;
    for (int i = 0; i < k; i++) t += a[i];
    sum = min(sum, t);
  }
  cout << sum;
  return 0;
}
