#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
const long double PI = (long double)(3.1415926535897932384626433832795);
void solve() {
  map<int, vector<int> > mp;
  int n, k;
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    int cnt = 0;
    mp[tmp].push_back(cnt);
    while (tmp) {
      tmp /= 2;
      cnt++;
      mp[tmp].push_back(cnt);
    }
  }
  long long mn = (long long)1e18;
  for (auto x : mp) {
    if (x.second.size() >= k) {
      long long tmp = 0;
      sort((x.second).begin(), (x.second).end());
      for (long long i = 0; i < k; ++i) {
        tmp += x.second[i];
      }
      mn = min(mn, tmp);
    }
  }
  cout << mn << endl;
}
int main() {
  long long t = 1;
  for (long long i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}
