#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long mx, p1, p2, val, sum, res, res1;
long long m[200007][27];
int main() {
  ios_base::sync_with_stdio();
  cin.tie();
  cout.tie();
  ;
  cin >> n >> k;
  res = 1000000007.0;
  for (int i = 1; i <= n; i++) {
    cin >> val;
    mx = max(val, mx);
    m[val][0]++;
    if (m[val][0] >= k) res = 0;
  }
  mx /= 2;
  for (long long i = mx; i >= 0; i--) {
    if (i == 0) {
      sum = m[0][0];
      for (int j = 0; j <= 19; j++) {
        m[0][j + 1] += m[1][j];
        sum += m[i][j + 1];
      }
    } else {
      p1 = 2 * i;
      p2 = 2 * i + 1;
      sum = m[i][0];
      for (int j = 0; j <= 19; j++) {
        m[i][j + 1] += m[p1][j] + m[p2][j];
        sum += m[i][j + 1];
      }
    }
    if (sum >= k) {
      sum = 0;
      res1 = 0;
      for (long long j = 0; j <= 19; j++) {
        res1 += m[i][j] * j;
        sum += m[i][j];
        if (sum >= k) {
          res = min(res, (res1 - (sum - k) * j));
          break;
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
