#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000;
const long long LINF = 1LL * INF * INF;
const int MAX = 100010;
const long double PI = acos(-1.);
const double EPS = 1e-6;
long long mod = 1000LL * 1000 * 1000 + 7;
int arr[200 * 1000 + 7];
vector<int> tmp[200 * 1000 + 7];
int main() {
  ios_base::sync_with_stdio(0);
  double startClock = clock();
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    int cnt = 0;
    while (arr[i]) {
      tmp[arr[i]].push_back(cnt);
      arr[i] /= 2;
      cnt++;
    }
    tmp[arr[i]].push_back(cnt);
  }
  int ans = INF;
  for (int i = 0; i < 200 * 1000 + 7; ++i) {
    if ((int)tmp[i].size() >= k) {
      sort(tmp[i].begin(), tmp[i].end());
      int sum = 0;
      for (int j = 0; j < k; ++j) sum += tmp[i][j];
      ans = min(ans, sum);
    }
  }
  cout << ans;
  cerr << endl << (clock() - startClock) / CLOCKS_PER_SEC << endl;
  return 0;
}
