#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> a[maxn];
int main() {
  int n, m, x, mx = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    mx = max(mx, x);
    for (int j = 0;; j++) {
      a[x].push_back(j);
      if (x == 0) break;
      x /= 2;
    }
  }
  int ans = 1e9;
  for (int i = 0; i <= mx; i++) {
    if ((int)a[i].size() < m) continue;
    sort(a[i].begin(), a[i].end());
    int tmp = 0;
    for (int j = 0; j < m; j++) {
      tmp += a[i][j];
    }
    ans = min(ans, tmp);
  }
  printf("%d\n", ans);
  return 0;
}
