#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int maxn = 20010;
const int INF = 0x3f3f3f3f;
const double eps = 0.0000001;
const long long mod = 1e9 + 7;
int n, m, x, y, z, k, cnt, t, len, q;
vector<int> a[N];
int main() {
  scanf("%d%d", &n, &k);
  int s;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s);
    for (int j = 0; s > 0; j++, s /= 2) {
      a[s].push_back(j);
    }
  }
  int ans = INF;
  for (int i = 1; i <= N - 1; i++) {
    sort(a[i].begin(), a[i].end());
    if (a[i].size() >= k) {
      int sum = 0;
      for (int j = 0; j < k; j++) {
        sum += a[i][j];
      }
      ans = min(ans, sum);
    }
  }
  printf("%d", ans);
}
