#include <bits/stdc++.h>
using namespace std;
long long n, m, k, i, j, a[500], ans = 99999, x;
map<long long, long long> cost, num;
int main() {
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  for (i = 1; i <= n; i++) {
    long long x = 0;
    while (a[i] != 0 && num[a[i]] < k) {
      cost[a[i]] += x;
      num[a[i]]++;
      a[i] /= 2;
      x++;
    }
  }
  for (auto p : num) {
    if (p.second == k) {
      ans = min(ans, cost[p.first]);
    }
  }
  cout << ans;
}
