#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here;
int n,k;
cin>>n>>k;
vector<int> p(n);
for(int i=0;i<n;i++){
cin>>p[i];
}
int sum=0,count=0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
unordered_map<int,int> mp;
for(int i=0;i<n;i++){
pq.push({0,p[i]});
// mp[p[i]]=0;
}
while(count<k+n&&!pq.empty()){
auto it = pq.top();
pq.pop();
int dis = it.first;
int loc = it.second;
if(mp[loc]==0){
sum+=dis;
if(mp[loc+1]==0){
pq.push({dis+1,loc+1});
}
if(mp[loc-1]==0){
pq.push({dis+1,loc-1});
}
count++;
mp[loc]=1;
}
}
cout<<sum;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmU7CglpbnQgbixrOwoJY2luPj5uPj5rOwoJdmVjdG9yPGludD4gcChuKTsKCWZvcihpbnQgaT0wO2k8bjtpKyspewoJCWNpbj4+cFtpXTsKCX0KCWludCBzdW09MCxjb3VudD0wOwoJIHByaW9yaXR5X3F1ZXVlPHBhaXI8aW50LCBpbnQ+LCB2ZWN0b3I8cGFpcjxpbnQsIGludD4+LCBncmVhdGVyPHBhaXI8aW50LCBpbnQ+Pj4gcHE7CiAgICAgdW5vcmRlcmVkX21hcDxpbnQsaW50PiBtcDsKICAgIGZvcihpbnQgaT0wO2k8bjtpKyspewogICAgCXBxLnB1c2goezAscFtpXX0pOwogICAgCS8vIG1wW3BbaV1dPTA7CiAgICB9CiAgICB3aGlsZShjb3VudDxrK24mJiFwcS5lbXB0eSgpKXsKICAgIAlhdXRvIGl0ID0gcHEudG9wKCk7CiAgICAJcHEucG9wKCk7CiAgICAJaW50IGRpcyA9IGl0LmZpcnN0OwogICAgCWludCBsb2MgPSBpdC5zZWNvbmQ7CiAgICAKICAgIAlpZihtcFtsb2NdPT0wKXsKICAgIAkJc3VtKz1kaXM7CiAgICAJCWlmKG1wW2xvYysxXT09MCl7CiAgICAJCQlwcS5wdXNoKHtkaXMrMSxsb2MrMX0pOwogICAgCQl9CiAgICAJCWlmKG1wW2xvYy0xXT09MCl7CiAgICAJCQlwcS5wdXNoKHtkaXMrMSxsb2MtMX0pOwogICAgCQl9CiAgICAJY291bnQrKzsKICAgIAltcFtsb2NdPTE7CiAgICAJfQogICAgCQogICAgfQogICAgY291dDw8c3VtOwoJcmV0dXJuIDA7Cn0=