#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
const double PI = acos(-1);
int go[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int n, k, p[N], num[N], a[N], ans = 0x3f3f3f3f;
void get(int x, int y) {
  p[x]++;
  num[x] += y;
  if (p[x] == k) ans = min(ans, num[x]);
  if (!x) return;
  get(x / 2, y + 1);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = (1); i <= (n); ++i) cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = (1); i <= (n); ++i) get(a[i], 0);
  cout << ans << '\n';
  return 0;
}
