#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MAX = 2e5 + 5;
const long long MAX2 = 11;
const long long MOD = 998244353;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 450;
int n, k, x, cnt, tmp, ans;
vector<int> v[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (long long i = 1; i <= n; ++i) {
    cin >> x;
    cnt = 0;
    while (x) {
      v[x].push_back(cnt);
      ++cnt;
      x >>= 1;
    }
    v[x].push_back(cnt);
  }
  ans = 1e9;
  for (long long i = 0; i <= 200000; ++i) {
    if (v[i].size() < k) continue;
    sort(v[i].begin(), v[i].end());
    tmp = 0;
    for (long long j = k - 1; j >= 0; --j) tmp += v[i][j];
    ans = min(ans, tmp);
  }
  cout << ans << "\n";
  return 0;
}
