#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 2e5 + 10;
int n, m, sum[N];
multiset<int> s[N];
inline void Add(int x, int y) {
  if (s[x].size() < m)
    s[x].insert(y), sum[x] += y;
  else {
    int tmp = *(--s[x].end());
    if (y < tmp)
      s[x].erase(s[x].find(tmp)), s[x].insert(y), sum[x] = sum[x] - tmp + y;
  }
}
int main() {
  n = read(), m = read();
  for (register int i = (1); i <= (n); i++) {
    int x = read();
    Add(x, 0);
    for (register int j = (1); j <= (20); j++) {
      x = x / 2, Add(x, j);
      if (!x) break;
    }
  }
  int ans = 1e9;
  for (register int i = (0); i <= (200000); i++)
    if (s[i].size() == m) ans = min(ans, sum[i]);
  printf("%d\n", ans);
}
