#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, i, j, K;
  long long A[51] = {0}, change[51] = {0}, c = 0, ans = 10000000, s = 0;
  cin >> N >> K;
  for (i = 0; i < N; i++) cin >> A[i];
  for (i = 0; i < 200001; i++) {
    s = 0;
    for (j = 0; j < N; j++) {
      c = 0;
      long long p = A[j];
      while (p > i) {
        p /= 2;
        c++;
      }
      if (p == i)
        change[j] = c;
      else
        change[j] = 1000000;
    }
    sort(change, change + N);
    for (j = 0; j < K; j++) s += change[j];
    ans = min(ans, s);
  }
  cout << ans;
  return 0;
}
