#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  static long long f[200001LL][2];
  for (int i = 0; i < 200001LL; ++i) {
    for (int j = 0; j < 2; ++j) {
      f[i][j] = 0;
    }
  }
  for (auto &e : a) {
    cin >> e;
    f[e][0]++;
  }
  f[200001LL - 1][1] = f[200001LL - 1][0];
  for (int i = 200001LL - 2; i >= 0; --i) {
    f[i][1] = f[i + 1][1] + f[i][0];
  }
  long long mnans = numeric_limits<long long>::max();
  for (int i = 1; i <= 200001LL - 1; ++i) {
    long long ans = 0;
    long long rem = max(0LL, k - f[i][0]);
    if (rem == 0) {
      mnans = 0;
      break;
    }
    for (int j = 1; i * pow(2, j) <= 200001LL - 1; ++j) {
      long long beg = i * pow(2, j);
      long long nd =
          min((long long)(pow(2, j) * i + pow(2, j) - 1LL), 200001LL - 1);
      long long contrib = f[beg][1] - f[nd][1] + f[nd][0];
      if (rem > contrib) {
        ans += contrib * j;
        rem -= contrib;
      } else {
        ans += rem * j;
        rem = 0;
        break;
      }
    }
    if (rem == 0) {
      mnans = min(mnans, ans);
    }
  }
  cout << mnans << endl;
}
