#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k, i, j, ans;
  cin >> n >> k;
  vector<long long> v(n);
  for (auto &it : v) {
    cin >> it;
  }
  sort(v.begin(), v.end());
  long long pre[n + 1];
  pre[0] = 0;
  for (i = 0; i < n; i++) {
    pre[i + 1] = pre[i] + v[i];
  }
  ans = LLONG_MAX;
  for (i = 0; i < n; i = j) {
    j = upper_bound(v.begin(), v.end(), v[i]) - v.begin();
    if (j - i >= k) {
      ans = 0;
      break;
    }
    if (i >= k - j + i) {
      ans = min(ans, k - j + i + ((v[i] - 1) * i) - pre[i]);
    }
    if (n - j >= k - j + i) {
      ans = min(ans, k - j + i - ((v[i] + 1) * (n - j)) + pre[n] - pre[j]);
    }
    ans = min(ans, k - j + i + ((v[i] - 1) * i) - pre[i] -
                       ((v[i] + 1) * (n - j)) + pre[n] - pre[j]);
  }
  cout << ans;
  return 0;
}
