#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[100], n, k, visit[N];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    int x = a[i];
    while (x) {
      visit[x] = 1;
      x /= 2;
    }
  }
  int ans = 1e9, b[N];
  for (int j = 0; j < N; j++) {
    int cnt = 0;
    if (visit[j]) {
      for (int i = 0; i < n; i++) {
        int x = a[i], k = 0;
        while (x > j) {
          x /= 2;
          k++;
        }
        if (x == j) {
          b[cnt++] = k;
        }
      }
      if (cnt < k) continue;
      sort(b, b + cnt);
      int p = 0;
      for (int i = 0; i < k; i++) {
        p += b[i];
      }
      ans = min(ans, p);
    }
  }
  printf("%d", ans);
  return 0;
}
