#include <bits/stdc++.h>
using namespace std;
int arr[200005];
map<int, int> mp;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  int ans = 1e9;
  for (int i = 0; i < n; i++) {
    int zz = v[i];
    int cnt = 0;
    mp[zz]++;
    if (mp[zz] == k) ans = min(ans, arr[zz]);
    while (zz > 0) {
      zz /= 2;
      mp[zz]++;
      cnt++;
      arr[zz] += cnt;
      if (mp[zz] == k) {
        ans = min(ans, arr[zz]);
      }
    }
  }
  cout << ans << endl;
}
