#include <bits/stdc++.h>
using namespace std;
string alpha = "abcdefghijklmnopqrstuvwxyz";
string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string glas = "aeiou";
long long int Mod = 1000 * 1000 * 1000 + 7;
long long int INF = 1000000000000000000;
vector<long long int> got[200001];
int main() {
  iostream::sync_with_stdio(false);
  cin.tie(0);
  long long int n, k;
  cin >> n >> k;
  vector<long long int> v[n];
  for (long long int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    while (x) {
      v[i].push_back(x);
      x /= 2;
    }
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < v[i].size(); j++) {
      got[v[i][j]].push_back(j);
    }
  }
  for (long long int i = 0; i < 200001; i++) {
    sort(got[i].begin(), got[i].end());
  }
  long long int ans = 9999999999999;
  for (long long int i = 0; i < 200001; i++) {
    if (got[i].size() < k) continue;
    long long int curr = 0;
    for (long long int j = 0; j < k; j++) {
      curr += got[i][j];
    }
    ans = min(curr, ans);
  }
  cout << ans;
}
