#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int INF = 0x3f3f3f3f;
int num[100];
int cnt[2][MAXN];
int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> num[i];
  sort(num, num + n);
  for (int i = 0; i < n; i++) {
    int cnt2 = 0;
    int b = num[i];
    while (b) {
      if (cnt[0][b] < k) {
        cnt[0][b]++;
        cnt[1][b] += cnt2;
      }
      b = b >> 1;
      cnt2++;
    }
  }
  int res = INF;
  for (int i = 0; i <= 200000; i++) {
    if (cnt[0][i] == k && cnt[1][i] < res) res = cnt[1][i];
  }
  printf("%d\n", res);
}
