#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
int a[MAXN];
int b[MAXN];
int num[MAXN];
int cnt;
int numl[MAXN];
int numr[MAXN];
ll suml[MAXN];
ll sumr[MAXN];

ll check(int i)
{
	int need=max(0,M-num[i]);
	ll ans;

	if(numl[i]>=need&&numr[i]>=need)
	{
		ans=min(suml[i],sumr[i])+need;
	}
	else if(numl[i]>=need)
	{
		ans=suml[i]+need;
	}
	else if(numr[i]>=need)
	{
		ans=sumr[i]+need;
	}
	else
	{
		ans=suml[i]+sumr[i]+need;
	}

	return need?ans:0;
}

void init()
{
	sort(a,a+N);
	cnt=0;
	b[0]=0;
	for(int i=0;i<N;i++)
	{
		if(a[i]==b[cnt])
		{
			num[cnt]++;
		}
		else
		{
			b[++cnt]=a[i];
			num[cnt]=1;
		}
	}
	for(int i=1;i<=cnt;i++)
	{
		numl[i]=(i-1>0?(numl[i-1]+num[i-1]):0);
		suml[i]=(i-1>0?suml[i-1]:0)+(i-1>0?numl[i-1]:0)+(i-1>0?numl[i]*(b[i]-b[i-1]-1):0);
	}
	for(int i=cnt;i>=1;i--)
	{
		numr[i]=(i+1<=cnt?(numr[i+1]+num[i+1]):0);
		sumr[i]=(i+1<=cnt?sumr[i+1]:0)+(i+1<=cnt?numr[i+1]:0)+(i+1<=cnt?numr[i]*(b[i+1]-b[i]-1):0);
	}
}

void solve()
{
	ll ans;

	scanf("%d%d",&N,&M);
	for(int i=0;i<N;i++)
	{
		a[i]=read();
	}
	init();
	ans=(ll)INF*INF;
	for(int i=1;i<=cnt;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
ll a[MAXN];

ll check(ll x)
{
	int cnt=M;
	ll ans=0;

	for(int i=0;i<N;i++)
	{
		if(a[i]==x)
		{
			cnt--;
		}
	}
	for(int i=0,j=N-1;cnt>0;cnt--)
	{
		if(a[i]<x&&a[j]>x)
		{
			if(x-a[i]<a[j]-x)
			{
				ans+=x-a[i];
				i++;
			}
			else
			{
				ans+=a[j]-x;
				j--;
			}
		}
		else if(a[i]<x)
		{
			ans+=x-a[i];
			i++;
		}
		else
		{
			ans+=a[j]-x;
			j--;
		}
	}

	return ans;
}

void solve()
{
	ll ans;
	ll l,r,m;
	ll x,y;
	ll ax,ay;

	scanf("%d%d",&N,&M);
	l=INF;
	r=0;
	ans=(ll)INF*INF;
	for(int i=0;i<N;i++)
	{
		a[i]=read();
		l=min(l,a[i]);
		r=max(r,a[i]);
	}
	sort(a,a+N);
	while(r-l>=3)
	{
		m=(r-l)/3;
		x=l+m;
		y=x+m;
		ax=check(x);
		ay=check(y);
		if(ax<ay)
		{
			r=y-1;
			ans=min(ans,ax);
		}
		else
		{
			l=x+1;
			ans=min(ans,ay);
		}
	}
	for(ll i=l;i<=r;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
int a[MAXN];
int b[MAXN];
int num[MAXN];
int cnt;
int numl[MAXN];
int numr[MAXN];
ll suml[MAXN];
ll sumr[MAXN];

ll check(int i)
{
	int need=max(0,M-num[i]);
	ll ans;

	if(numl[i]>=need&&numr[i]>=need)
	{
		ans=min(suml[i],sumr[i])+need;
	}
	else if(numl[i]>=need)
	{
		ans=suml[i]+need;
	}
	else if(numr[i]>=need)
	{
		ans=sumr[i]+need;
	}
	else
	{
		ans=suml[i]+sumr[i]+need;
	}

	return need?ans:0;
}

void init()
{
	sort(a,a+N);
	cnt=0;
	b[0]=0;
	for(int i=0;i<N;i++)
	{
		if(a[i]==b[cnt])
		{
			num[cnt]++;
		}
		else
		{
			b[++cnt]=a[i];
			num[cnt]=1;
		}
	}
	for(int i=1;i<=cnt;i++)
	{
		numl[i]=(i-1>0?(numl[i-1]+num[i-1]):0);
		suml[i]=(i-1>0?suml[i-1]:0)+(i-1>0?numl[i-1]:0)+(i-1>0?(ll)numl[i]*(b[i]-b[i-1]-1):0);
	}
	for(int i=cnt;i>=1;i--)
	{
		numr[i]=(i+1<=cnt?(numr[i+1]+num[i+1]):0);
		sumr[i]=(i+1<=cnt?sumr[i+1]:0)+(i+1<=cnt?numr[i+1]:0)+(i+1<=cnt?(ll)numr[i]*(b[i+1]-b[i]-1):0);
	}
}

void solve()
{
	ll ans;

	scanf("%d%d",&N,&M);
	for(int i=0;i<N;i++)
	{
		a[i]=read();
	}
	init();
	ans=(ll)INF*INF;
	for(int i=1;i<=cnt;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
ll a[MAXN];

ll check(ll x)
{
	int cnt=M;
	ll ans=0;

	for(int i=0;i<N;i++)
	{
		if(a[i]==x)
		{
			cnt--;
		}
	}
	for(int i=0,j=N-1;cnt>0;cnt--)
	{
		if(a[i]<x&&a[j]>x)
		{
			if(x-a[i]<a[j]-x)
			{
				ans+=x-a[i];
				i++;
			}
			else
			{
				ans+=a[j]-x;
				j--;
			}
		}
		else if(a[i]<x)
		{
			ans+=x-a[i];
			i++;
		}
		else
		{
			ans+=a[j]-x;
			j--;
		}
	}

	return ans;
}

void solve()
{
	ll ans;
	ll l,r,m;
	ll x,y;

	scanf("%d%d",&N,&M);
	l=INF;
	r=0;
	ans=(ll)INF*INF;
	for(int i=0;i<N;i++)
	{
		a[i]=read();
		l=min(l,a[i]);
		r=max(r,a[i]);
	}
	sort(a,a+N);
	while(r-l>=3)
	{
		m=(r-l)/3;
		x=l+m;
		y=x+m;
		if(check(x)<check(y))
		{
			r=y-1;
		}
		else
		{
			l=x+1;
		}
	}
	for(ll i=l;i<=r;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
ll a[MAXN];

ll check(ll x)
{
	int cnt=M;
	int numl,numr;
	ll suml,sumr;
	ll ans=0;

	for(int i=0;i<N;i++)
	{
		if(a[i]==x)
		{
			cnt--;
		}
	}
	numl=0;
	suml=0;
	for(int i=0;i<N;i++)
	{
		if(a[i]<x)
		{
			numl++;
			suml+=x-a[i]-1;
		}
		else
		{
			break;
		}
	}
	numr=0;
	sumr=0;
	for(int i=N-1;i>=0;i--)
	{
		if(a[i]>x)
		{
			numr++;
			sumr+=a[i]-x-1;
		}
		else
		{
			break;
		}
	}
	if(numl>cnt&&numr>cnt)
	{
		ans=min(suml,sumr)+cnt;
	}
	else if(numl>cnt)
	{
		ans=suml+cnt;
	}
	else if(numr>cnt)
	{
		ans=sumr+cnt;
	}
	else
	{
		ans=suml+sumr+cnt;
	}

	return ans;
}

void solve()
{
	ll ans;
	ll l,r,m;
	ll x,y;
	ll ax,ay;

	scanf("%d%d",&N,&M);
	l=INF;
	r=0;
	ans=(ll)INF*INF;
	for(int i=0;i<N;i++)
	{
		a[i]=read();
		l=min(l,a[i]);
		r=max(r,a[i]);
	}
	sort(a,a+N);
	while(r-l>=3)
	{
		m=(r-l)/3;
		x=l+m;
		y=x+m;
		ax=check(x);
		ay=check(y);
		if(ax<ay)
		{
			r=y-1;
			ans=min(ans,ax);
		}
		else
		{
			l=x+1;
			ans=min(ans,ay);
		}
	}
	for(ll i=l;i<=r;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
ll a[MAXN];

ll check(ll x)
{
	int cnt=M;
	int numl,numr;
	ll suml,sumr;
	ll ans=0;

	for(int i=0;i<N;i++)
	{
		if(a[i]==x)
		{
			cnt--;
		}
	}
	numl=0;
	suml=0;
	for(int i=0;i<N;i++)
	{
		if(a[i]<x)
		{
			numl++;
			suml+=x-a[i]-1;
		}
		else
		{
			break;
		}
	}
	numr=0;
	sumr=0;
	for(int i=N-1;i>=0;i--)
	{
		if(a[i]>x)
		{
			numr++;
			sumr+=a[i]-x-1;
		}
		else
		{
			break;
		}
	}
	if(numl>cnt&&numr>cnt)
	{
		ans=min(suml,sumr)+cnt;
	}
	else if(numl>cnt)
	{
		ans=suml+cnt;
	}
	else if(numr>cnt)
	{
		ans=sumr+cnt;
	}
	else
	{
		ans=suml+sumr+cnt;
	}

	return cnt>0?ans:0;
}

void solve()
{
	ll ans;
	ll l,r,m;
	ll x,y;
	ll ax,ay;

	scanf("%d%d",&N,&M);
	l=INF;
	r=0;
	ans=(ll)INF*INF;
	for(int i=0;i<N;i++)
	{
		a[i]=read();
		l=min(l,a[i]);
		r=max(r,a[i]);
	}
	sort(a,a+N);
	while(r-l>=3)
	{
		m=(r-l)/3;
		x=l+m;
		y=x+m;
		ax=check(x);
		ay=check(y);
		if(ax<ay)
		{
			r=y-1;
			ans=min(ans,ax);
		}
		else
		{
			l=x+1;
			ans=min(ans,ay);
		}
	}
	for(ll i=l;i<=r;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#include<time.h>
#define ZERO 1e-6
#define pi acos(-1)
#define pii pair<ll,int>
#define gc getchar()
#define fi first
#define se second
#define ls <<1
#define rs <<1|1
#define test printf("!\n");
#define te(x) printf("%s\n",(x));
#define pr(x) printf("%d ",(x));
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=998244353;
const int INF=0x3f3f3f3f;

ll read()
{
	ll x=0,f=1;
	char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}

int N,M;
ll a[MAXN];

ll check(ll x)
{
	int cnt=M;
	int numl,numr;
	ll suml,sumr;
	ll ans=0;

	for(int i=0;i<N;i++)
	{
		if(a[i]==x)
		{
			cnt--;
		}
	}
	numl=0;
	suml=0;
	for(int i=0;i<N;i++)
	{
		if(a[i]<x)
		{
			numl++;
			suml+=x-a[i]-1;
		}
		else
		{
			break;
		}
	}
	numr=0;
	sumr=0;
	for(int i=N-1;i>=0;i--)
	{
		if(a[i]>x)
		{
			numr++;
			sumr+=a[i]-x-1;
		}
		else
		{
			break;
		}
	}
	if(numl>=cnt&&numr>=cnt)
	{
		ans=min(suml,sumr)+cnt;
	}
	else if(numl>=cnt)
	{
		ans=suml+cnt;
	}
	else if(numr>=cnt)
	{
		ans=sumr+cnt;
	}
	else
	{
		ans=suml+sumr+cnt;
	}

	return cnt>0?ans:0;
}

void solve()
{
	ll ans;
	ll l,r,m;
	ll x,y;
	ll ax,ay;

	scanf("%d%d",&N,&M);
	l=INF;
	r=0;
	ans=(ll)INF*INF;
	for(int i=0;i<N;i++)
	{
		a[i]=read();
		l=min(l,a[i]);
		r=max(r,a[i]);
	}
	sort(a,a+N);
	while(r-l>=3)
	{
		m=(r-l)/3;
		x=l+m;
		y=x+m;
		ax=check(x);
		ay=check(y);
		if(ax<ay)
		{
			r=y-1;
			ans=min(ans,ax);
		}
		else
		{
			l=x+1;
			ans=min(ans,ay);
		}
	}
	for(ll i=l;i<=r;i++)
	{
		ans=min(ans,check(i));
	}
	printf("%lld\n",ans);
}

int main()
{
	int T;

	T=1;
//	scanf("%d",&T);
	while(T--)
	{
		solve();
	}

	return 0;
}
/*

*/


#include <bits/stdc++.h>

using namespace std;

#define fastio cin.tie(NULL);cout.tie(NULL);ios_base::sync_with_stdio(0);
#define debug(x) cout << x << endl;
#define endl '\n'
#define sz(x) (int)x.size()
#define popcount __builtin_popcount
#define MAXN 200005
#define LOG 20
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define pb push_back
#define int long long

typedef long long ll;
typedef pair <ll, ll> pii;
typedef vector <int> vi;
typedef vector <pii> vii;

int n, k;
int nums[MAXN];
int prefixQtd[MAXN];
int prefixSum[MAXN];
int suffixQtd[MAXN];
int suffixSum[MAXN];

int32_t main(){
    fastio
    cin >> n >> k;
    vii elements;

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }

    for(int i = 1; i <= n; i++) cin >> nums[i];
    sort(nums + 1, nums + n + 1);

    int qtd = 1;

    for(int i = 2; i <= n; i++){
        if(nums[i] != nums[i - 1]){
            elements.pb({nums[i - 1], qtd});
            qtd = 0;
        }
        if(i == n) elements.pb({nums[i], qtd + 1});
        qtd++;
    }

    int elSz = sz(elements) - 1;

    prefixQtd[0] = elements[0].second;

    for(int i = 1; i <= elSz; i++){
        prefixSum[i] = prefixQtd[i - 1] * (elements[i].first - elements[i - 1].first) + prefixSum[i - 1];
        prefixQtd[i] = prefixQtd[i - 1] + elements[i].second;
    }

    suffixQtd[elSz] = elements[elSz].second;

    for(int i = elSz - 1; i >= 0; i--){
        suffixSum[i] = suffixQtd[i + 1] * (elements[i + 1].first - elements[i].first) + suffixSum[i + 1];
        suffixQtd[i] = suffixQtd[i + 1] + elements[i].second;
    }

    int res = INF;
    int atual1, atual2, extra;

    for(int i = 0; i <= elSz; i++){
        atual1 = atual2 = INF;
        if(elements[i].second >= k) res = 0;
        else {
            if(prefixQtd[i] >= k && i > 0){
                extra = prefixQtd[i] - k;
                atual1 = prefixSum[i] - extra;
            } else if(i < elSz){
                atual1 = prefixSum[i];
                extra = n - k;
                atual1 += suffixSum[i] - extra;
            }
            if(suffixQtd[i] >= k && i < elSz){
                extra = suffixQtd[i] - k;
                atual2 = suffixSum[i] - extra;
            } else if(i > 0){
                atual2 = suffixSum[i];
                extra = n - k;
                atual2 += prefixSum[i] - extra;
            }
        }
        res = min({res, atual1, atual2});
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fastio cin.tie(NULL);cout.tie(NULL);ios_base::sync_with_stdio(0);
#define debug(x) cout << x << endl;
#define endl '\n'
#define sz(x) (int)x.size()
#define popcount __builtin_popcount
#define MAXN 200005
#define LOG 20
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define pb push_back
#define int long long

typedef long long ll;
typedef pair <ll, ll> pii;
typedef vector <int> vi;
typedef vector <pii> vii;

int n, k;
int nums[MAXN];
int prefixQtd[MAXN];
int prefixSum[MAXN];
int suffixQtd[MAXN];
int suffixSum[MAXN];

int32_t main(){
    fastio
    cin >> n >> k;
    vii elements;
    int a;

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }

    for(int i = 1; i <= n; i++) cin >> nums[i];
    sort(nums + 1, nums + n + 1);

    int qtd = 1;

    for(int i = 2; i <= n; i++){
        if(nums[i] != nums[i - 1]){
            elements.pb({nums[i - 1], qtd});
            qtd = 0;
        }
        if(i == n) elements.pb({nums[i], qtd + 1});
        qtd++;
    }

    int elSz = sz(elements) - 1;

    prefixQtd[0] = elements[0].second;

    for(int i = 1; i <= elSz; i++){
        prefixSum[i] = prefixQtd[i - 1] * (elements[i].first - elements[i - 1].first) + prefixSum[i - 1];
        prefixQtd[i] = prefixQtd[i - 1] + elements[i].second;
    }

    suffixQtd[elSz] = elements[elSz].second;

    for(int i = elSz - 1; i >= 0; i--){
        suffixSum[i] = suffixQtd[i + 1] * (elements[i + 1].first - elements[i].first) + suffixSum[i + 1];
        suffixQtd[i] = suffixQtd[i + 1] + elements[i].second;
    }

    int res = INF;
    int atual, extra;

    for(int i = 0; i <= elSz; i++){
        if(elements[i].second >= k) res = 0;
        else if((elements[i].first - elements[0].first) <= (elements[elSz].first - elements[i].first)){
            if(prefixQtd[i] >= k){
                extra = (prefixQtd[i] - k) * (elements[i].first - elements[i - 1].first);
                atual = prefixSum[i] - extra;
            } else {
                atual = prefixSum[i];
                extra = (n - k) * (elements[i + 1].first - elements[i].first);
                atual += suffixSum[i] - extra;
            }
        } else {
            if(suffixQtd[i] >= k){
                extra = (suffixQtd[i] - k) * (elements[i + 1].first - elements[i].first);
                atual = suffixSum[i] - extra;
            } else {
                atual = suffixSum[i];
                extra = (n - k) * (elements[i].first - elements[i - 1].first);
                atual += prefixSum[i] - extra;
            }
        }
        res = min(res, atual);
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fastio cin.tie(NULL);cout.tie(NULL);ios_base::sync_with_stdio(0);
#define debug(x) cout << x << endl;
#define endl '\n'
#define sz(x) (int)x.size()
#define popcount __builtin_popcount
#define MAXN 200005
#define LOG 20
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define pb push_back
#define int long long

typedef long long ll;
typedef pair <ll, ll> pii;
typedef vector <int> vi;
typedef vector <pii> vii;

int n, k;
int nums[MAXN];
int prefixQtd[MAXN];
int prefixSum[MAXN];
int suffixQtd[MAXN];
int suffixSum[MAXN];

int32_t main(){
    fastio
    cin >> n >> k;
    vii elements;
    int a;

    for(int i = 1; i <= n; i++) cin >> nums[i];
    sort(nums + 1, nums + n + 1);

    int qtd = 1;

    for(int i = 2; i <= n; i++){
        if(nums[i] != nums[i - 1]){
            elements.pb({nums[i - 1], qtd});
            qtd = 0;
        }
        if(i == n) elements.pb({nums[i], qtd + 1});
        qtd++;
    }

    int elSz = sz(elements) - 1;

    prefixQtd[0] = elements[0].second;

    for(int i = 1; i <= elSz; i++){
        prefixSum[i] = prefixQtd[i - 1] * (elements[i].first - elements[i - 1].first) + prefixSum[i - 1];
        prefixQtd[i] = prefixQtd[i - 1] + elements[i].second;
    }

    suffixQtd[elSz] = elements[elSz].second;

    for(int i = elSz - 1; i >= 0; i--){
        suffixSum[i] = suffixQtd[i + 1] * (elements[i + 1].first - elements[i].first) + suffixSum[i + 1];
        suffixQtd[i] = suffixQtd[i + 1] + elements[i].second;
    }

    int res = INF;
    int atual, extra;

    for(int i = 0; i <= elSz; i++){
        if(elements[i].second >= k) res = 0;
        else if((elements[i].first - elements[0].first) <= (elements[elSz].first - elements[i].first)){
            if(prefixQtd[i] >= k){
                extra = (prefixQtd[i] - k) * (elements[i].first - elements[i - 1].first);
                atual = prefixSum[i] - extra;
            } else {
                atual = prefixSum[i];
                extra = (n - k) * (elements[i + 1].first - elements[i].first);
                atual += suffixSum[i] - extra;
            }
        } else {
            if(suffixQtd[i] >= k){
                extra = (suffixQtd[i] - k) * (elements[i + 1].first - elements[i].first);
                atual = suffixSum[i] - extra;
            } else {
                atual = suffixSum[i];
                extra = (n - k) * (elements[i].first - elements[i - 1].first);
                atual += prefixSum[i] - extra;
            }
        }
        res = min(res, atual);
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fastio cin.tie(NULL);cout.tie(NULL);ios_base::sync_with_stdio(0);
#define debug(x) cout << x << endl;
#define endl '\n'
#define sz(x) (int)x.size()
#define popcount __builtin_popcount
#define MAXN 200005
#define LOG 20
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define pb push_back
#define int long long

typedef long long ll;
typedef pair <ll, ll> pii;
typedef vector <int> vi;
typedef vector <pii> vii;

int n, k;
int nums[MAXN];
int prefixQtd[MAXN];
int prefixSum[MAXN];
int suffixQtd[MAXN];
int suffixSum[MAXN];

int32_t main(){
    fastio
    cin >> n >> k;
    vii elements;
    int a;

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }

    for(int i = 1; i <= n; i++) cin >> nums[i];
    sort(nums + 1, nums + n + 1);

    int qtd = 1;

    for(int i = 2; i <= n; i++){
        if(nums[i] != nums[i - 1]){
            elements.pb({nums[i - 1], qtd});
            qtd = 0;
        }
        if(i == n) elements.pb({nums[i], qtd + 1});
        qtd++;
    }

    int elSz = sz(elements) - 1;

    prefixQtd[0] = elements[0].second;

    for(int i = 1; i <= elSz; i++){
        prefixSum[i] = prefixQtd[i - 1] * (elements[i].first - elements[i - 1].first) + prefixSum[i - 1];
        prefixQtd[i] = prefixQtd[i - 1] + elements[i].second;
    }

    suffixQtd[elSz] = elements[elSz].second;

    for(int i = elSz - 1; i >= 0; i--){
        suffixSum[i] = suffixQtd[i + 1] * (elements[i + 1].first - elements[i].first) + suffixSum[i + 1];
        suffixQtd[i] = suffixQtd[i + 1] + elements[i].second;
    }

    int res = INF;
    int atual1, atual2, extra;

    for(int i = 0; i <= elSz; i++){
        atual1 = atual2 = INF;
        if(elements[i].second >= k) res = 0;
        else {
            if(prefixQtd[i] >= k && i > 0){
                extra = (prefixQtd[i] - k) * (elements[i].first - elements[i - 1].first);
                atual1 = prefixSum[i] - extra;
            } else if(i < elSz){
                atual1 = prefixSum[i];
                extra = (n - k) * (elements[i + 1].first - elements[i].first);
                atual1 += suffixSum[i] - extra;
            }
            if(suffixQtd[i] >= k && i < elSz){
                extra = (suffixQtd[i] - k) * (elements[i + 1].first - elements[i].first);
                atual2 = suffixSum[i] - extra;
            } else if(i > 0){
                atual2 = suffixSum[i];
                extra = (n - k) * (elements[i].first - elements[i - 1].first);
                atual2 += prefixSum[i] - extra;
            }
        }
        res = min({res, atual1, atual2});
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fastio cin.tie(NULL);cout.tie(NULL);ios_base::sync_with_stdio(0);
#define debug(x) cout << x << endl;
#define endl '\n'
#define sz(x) (int)x.size()
#define popcount __builtin_popcount
#define MAXN 200005
#define LOG 20
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define pb push_back
#define int long long

typedef long long ll;
typedef pair <ll, ll> pii;
typedef vector <int> vi;
typedef vector <pii> vii;

int n, k;
int nums[MAXN];
int prefixQtd[MAXN];
int prefixSum[MAXN];
int suffixQtd[MAXN];
int suffixSum[MAXN];

int32_t main(){
    fastio
    cin >> n >> k;
    vii elements;
    int a;

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }

    for(int i = 1; i <= n; i++) cin >> nums[i];
    sort(nums + 1, nums + n + 1);

    int qtd = 1;

    for(int i = 2; i <= n; i++){
        if(nums[i] != nums[i - 1]){
            elements.pb({nums[i - 1], qtd});
            qtd = 0;
        }
        if(i == n) elements.pb({nums[i], qtd + 1});
        qtd++;
    }

    int elSz = sz(elements) - 1;

    prefixQtd[0] = elements[0].second;

    for(int i = 1; i <= elSz; i++){
        prefixSum[i] = prefixQtd[i - 1] * (elements[i].first - elements[i - 1].first) + prefixSum[i - 1];
        prefixQtd[i] = prefixQtd[i - 1] + elements[i].second;
    }

    suffixQtd[elSz] = elements[elSz].second;

    for(int i = elSz - 1; i >= 0; i--){
        suffixSum[i] = suffixQtd[i + 1] * (elements[i + 1].first - elements[i].first) + suffixSum[i + 1];
        suffixQtd[i] = suffixQtd[i + 1] + elements[i].second;
    }

    int res = INF;
    int atual, extra;

    for(int i = 0; i <= elSz; i++){
        if(elements[i].second >= k) res = 0;
        else if(prefixQtd[i] >= suffixQtd[i]){
            if(prefixQtd[i] >= k){
                extra = (prefixQtd[i] - k) * (elements[i].first - elements[i - 1].first);
                atual = prefixSum[i] - extra;
            } else {
                atual = prefixSum[i];
                extra = (n - k) * (elements[i + 1].first - elements[i].first);
                atual += suffixSum[i] - extra;
            }
        } else {
            if(suffixQtd[i] >= k){
                extra = (suffixQtd[i] - k) * (elements[i + 1].first - elements[i].first);
                atual = suffixSum[i] - extra;
            } else {
                atual = suffixSum[i];
                extra = (n - k) * (elements[i].first - elements[i - 1].first);
                atual += prefixSum[i] - extra;
            }
        }
        res = min(res, atual);
    }

    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 2e5 + 100;

int n, k;
ll a[N];
vector <pll> unq;
ll pfx[N], sfx[N];

int main() {
	fast_cin();
	cin >> n >> k;
	map <int, int> mp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] ++;
	}
	sort(a + 1, a + n + 1);

	for(int i = 1; i <= n; i++) {
		pfx[i] = pfx[i - 1] + a[i];
		trace(i, a[i]);
		if(a[i] != a[i - 1]) unq.pb({a[i], i});
	}
	for(int i = n; i >= 1; i--) {
		sfx[i] = sfx[i + 1] + a[i];
	}
	if(unq.size() == 1) {
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	ll ans = inf;
	for(int ui = 0; ui < unq.size(); ui++) {
		while(unq[ui].F > a[i]) i++;
		ll num = unq[ui].F;
		int knew = k - mp[a[i]];

		ll opsRight, cr;
		int ni = unq[ui + 1].S;
		ll rnum;
		if(ui < unq.size() - 1) {
			rnum = unq[ui + 1].F;
		} else {
			rnum = inf;
		}
		if(ui < unq.size() - 1) {
			opsRight = sfx[ni] - (rnum * (n - ni + 1));
		} else {
			opsRight = 0;
		}
		cr = n - ni + 1;

		ll opsLeft, cl;
		int li = unq[ui - 1].S;
		ll lnum;
		if(ui > 0) {
			lnum = unq[ui - 1].F;
		} else {
			lnum = -inf;
		}
		if(ui > 0) {
			opsLeft = (lnum * (li)) - pfx[li];
		} else {
			opsLeft = 0;
		}
		cl = i - 1;
		trace(i, opsRight, opsLeft, li, ni, lnum, rnum);

		if(cr >= knew and rnum != inf) {
			ll candidate = opsRight + knew * (rnum - num);
			ans = min(candidate, ans);
		}

		if(cl >= knew and lnum != -inf) {
			ll candidate = opsLeft + knew * (num - lnum);
			ans = min(ans, candidate);
		}

		if(ui > 0 and ui < unq.size() - 1) {
			ll ldiff = num - lnum, rdiff = rnum - num;

			if(num - lnum > rnum - num) {
				swap(ldiff, rdiff);
				swap(cl, cr);
			}
			ll candidate = cl * ldiff + opsLeft + opsRight;
			if(cl < knew) {
				candidate += (knew - cl) * rdiff;
			}
			ans = min(ans, candidate);
		}
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 2e18;
const int N = 2e5 + 100;

int n, k;
ll a[N];
vector <pll> unq;
ll pfx[N], sfx[N];

int main() {
	fast_cin();
	cin >> n >> k;
	map <int, int> mp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] ++;
	}
	sort(a + 1, a + n + 1);

	for(int i = 1; i <= n; i++) {
		pfx[i] = pfx[i - 1] + a[i];
		trace(i, a[i]);
		if(a[i] != a[i - 1]) unq.pb({a[i], i});
	}
	for(int i = n; i >= 1; i--) {
		sfx[i] = sfx[i + 1] + a[i];
	}
	if(unq.size() == 1) {
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	ll ans = inf;
	for(int ui = 0; ui < unq.size(); ui++) {
		while(i <= n and unq[ui].F > a[i]) i++;
		ll num = unq[ui].F;
		int knew = k - mp[a[i]];
		if(knew <= 0) {
			ans = 0;
			break;
		}

		ll opsRight, cr;
		int ni = unq[ui + 1].S;
		ll rnum;
		if(ui < unq.size() - 1) {
			rnum = unq[ui + 1].F;
		} else {
			rnum = inf;
		}
		if(ui < unq.size() - 1) {
			opsRight = sfx[ni] - (rnum * (n - ni + 1));
		} else {
			opsRight = 0;
		}
		cr = n - ni + 1;

		ll opsLeft, cl;
		int li = unq[ui - 1].S;
		ll lnum;
		if(ui > 0) {
			lnum = unq[ui - 1].F;
		} else {
			lnum = -inf;
		}
		if(ui > 0) {
			opsLeft = (lnum * (li)) - pfx[li];
		} else {
			opsLeft = 0;
		}
		cl = i - 1;
		trace(i, opsRight, opsLeft, li, ni, lnum, rnum);

		if(cr >= knew and rnum != inf) {
			ll candidate = opsRight + knew * (rnum - num);
			ans = min(candidate, ans);
		}

		if(cl >= knew and lnum != -inf) {
			ll candidate = opsLeft + knew * (num - lnum);
			ans = min(ans, candidate);
		}

		if(ui > 0 and ui < unq.size() - 1) {
			ll ldiff = num - lnum, rdiff = rnum - num;

			if(num - lnum > rnum - num) {
				swap(ldiff, rdiff);
				swap(cl, cr);
			}
			ll candidate = cl * ldiff + opsLeft + opsRight;
			if(cl < knew) {
				candidate += (knew - cl) * rdiff;
			}
			ans = min(ans, candidate);
		}
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 2e5 + 100;

int n, k;
ll a[N];
vector <pll> unq;
ll pfx[N], sfx[N];

int main() {
	fast_cin();
	cin >> n >> k;
	map <int, int> mp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] ++;
	}
	sort(a + 1, a + n + 1);

	for(int i = 1; i <= n; i++) {
		pfx[i] = pfx[i - 1] + a[i];
		trace(i, a[i]);
		if(a[i] != a[i - 1]) unq.pb({a[i], i});
	}
	for(int i = n; i >= 1; i--) {
		sfx[i] = sfx[i + 1] + a[i];
	}
	if(unq.size() == 1) {
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	ll ans = inf;
	for(int ui = 0; ui < unq.size(); ui++) {
		while(unq[ui].F > a[i]) i++;
		ll num = unq[ui].F;
		int knew = k - mp[a[i]];
		if(knew <= 0) {
			ans = 0;
			break;
		}

		ll opsRight, cr;
		int ni = unq[ui + 1].S;
		ll rnum;
		if(ui < unq.size() - 1) {
			rnum = unq[ui + 1].F;
		} else {
			rnum = inf;
		}
		if(ui < unq.size() - 1) {
			opsRight = sfx[ni] - (rnum * (n - ni + 1));
		} else {
			opsRight = 0;
		}
		cr = n - ni + 1;

		ll opsLeft, cl;
		int li = unq[ui - 1].S;
		ll lnum;
		if(ui > 0) {
			lnum = unq[ui - 1].F;
		} else {
			lnum = -inf;
		}
		if(ui > 0) {
			opsLeft = (lnum * (li)) - pfx[li];
		} else {
			opsLeft = 0;
		}
		cl = i - 1;
		trace(i, opsRight, opsLeft, li, ni, lnum, rnum);

		if(cr >= knew and rnum != inf) {
			ll candidate = opsRight + knew * (rnum - num);
			ans = min(candidate, ans);
		}

		if(cl >= knew and lnum != -inf) {
			ll candidate = opsLeft + knew * (num - lnum);
			ans = min(ans, candidate);
		}

		if(ui > 0 and ui < unq.size() - 1) {
			ll ldiff = num - lnum, rdiff = rnum - num;

			if(num - lnum > rnum - num) {
				swap(ldiff, rdiff);
				swap(cl, cr);
			}
			ll candidate = cl * ldiff + opsLeft + opsRight;
			if(cl < knew) {
				candidate += (knew - cl) * rdiff;
			}
			ans = min(ans, candidate);
		}
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 2e5 + 100;

int n, k;
ll a[N];
vector <pll> unq;
ll pfx[N], sfx[N];

int main() {
	fast_cin();
	cin >> n >> k;
	map <int, int> mp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] ++;
	}
	sort(a + 1, a + n + 1);

	for(int i = 1; i <= n; i++) {
		pfx[i] = pfx[i - 1] + a[i];
		trace(i, a[i]);
		if(a[i] != a[i - 1]) unq.pb({a[i], i});
	}
	for(int i = n; i >= 1; i--) {
		sfx[i] = sfx[i + 1] + a[i];
	}
	if(unq.size() == 1) {
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	ll ans = inf;
	for(int ui = 0; ui < unq.size(); ui++) {
		while(unq[ui].F > a[i]) i++;
		ll num = unq[ui].F;
		int knew = k - mp[a[i]];

		ll opsRight, cr;
		int ni = unq[ui + 1].S;
		ll rnum;
		if(ui < unq.size() - 1) {
			rnum = unq[ui + 1].F;
		} else {
			rnum = inf;
		}
		if(ui < unq.size() - 1) {
			opsRight = sfx[ni] - (rnum * (n - ni + 1));
		} else {
			opsRight = 0;
		}
		cr = n - ni + 1;

		ll opsLeft, cl;
		int li = unq[ui - 1].S;
		ll lnum;
		if(ui > 0) {
			lnum = unq[ui - 1].F;
		} else {
			lnum = -inf;
		}
		if(ui > 0) {
			opsLeft = (lnum * (li)) - pfx[li];
		} else {
			opsLeft = 0;
		}
		cl = i - 1;
		trace(i, opsRight, opsLeft, li, ni, lnum, rnum);

		if(cr >= knew and rnum != inf) {
			ll candidate = opsRight + knew * (rnum - num);
			ans = min(candidate, ans);
		}

		if(cl >= knew and lnum != -inf) {
			ll candidate = opsLeft + knew * (num - lnum);
			ans = min(ans, candidate);
		}

		ll ldiff = num - lnum, rdiff = rnum - num;

		if(num - lnum > rnum - num) {
			swap(ldiff, rdiff);
			swap(cl, cr);

		}
		ll candidate = cl * ldiff + opsLeft + opsRight;
		if(cl < knew) {
			candidate += (knew - cl) * rdiff;
		}
		ans = min(ans, candidate);
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;
const int N = 2e5 + 100;

int n, k;
ll a[N];
vector <pll> unq;
ll pfx[N], sfx[N];

int main() {
	fast_cin();
	cin >> n >> k;
	map <int, int> mp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] ++;
	}
	sort(a + 1, a + n + 1);

	for(int i = 1; i <= n; i++) {
		pfx[i] = pfx[i - 1] + a[i];
		trace(i, a[i]);
		if(a[i] != a[i - 1]) unq.pb({a[i], i});
	}
	for(int i = n; i >= 1; i--) {
		sfx[i] = sfx[i + 1] + a[i];
	}
	if(unq.size() == 1) {
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	ll ans = inf;
	for(int ui = 0; ui < unq.size(); ui++) {
		while(unq[ui].F > a[i]) i++;
		ll num = unq[ui].F;
		int knew = k - mp[a[i]];

		ll opsRight, cr;
		int ni = unq[ui + 1].S;
		ll rnum;
		if(ui < unq.size() - 1) {
			rnum = unq[ui + 1].F;
		} else {
			rnum = inf;
		}
		if(ui < unq.size() - 1) {
			opsRight = sfx[ni] - (rnum * (n - ni + 1));
		} else {
			opsRight = 0;
		}
		cr = n - ni + 1;

		ll opsLeft, cl;
		int li = unq[ui - 1].S;
		ll lnum;
		if(ui > 0) {
			lnum = unq[ui - 1].F;
		} else {
			lnum = -inf;
		}
		if(ui > 0) {
			opsLeft = (lnum * (li)) - pfx[li];
		} else {
			opsLeft = 0;
		}
		cl = i - 1;
		trace(i, opsRight, opsLeft, li, ni, lnum, rnum);

		if(cr >= knew and rnum != inf) {
			ll candidate = opsRight + knew * (rnum - num);
			ans = min(candidate, ans);
		}

		if(cl >= knew and lnum != -inf) {
			ll candidate = opsLeft + knew * (num - lnum);
			ans = min(ans, candidate);
		}

		if(i > 0 and i < unq.size() - 1) {
			ll ldiff = num - lnum, rdiff = rnum - num;

			if(num - lnum > rnum - num) {
				swap(ldiff, rdiff);
				swap(cl, cr);
			}
			ll candidate = cl * ldiff + opsLeft + opsRight;
			if(cl < knew) {
				candidate += (knew - cl) * rdiff;
			}
			ans = min(ans, candidate);
		}
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 2e18;
const int N = 2e5 + 100;

int n, k;
ll a[N];
vector <pll> unq;
ll pfx[N], sfx[N];

int main() {
	fast_cin();
	cin >> n >> k;
	map <int, int> mp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] ++;
	}
	sort(a + 1, a + n + 1);

	for(int i = 1; i <= n; i++) {
		pfx[i] = pfx[i - 1] + a[i];
		trace(i, a[i]);
		if(a[i] != a[i - 1]) unq.pb({a[i], i});
	}
	for(int i = n; i >= 1; i--) {
		sfx[i] = sfx[i + 1] + a[i];
	}
	if(unq.size() == 1) {
		cout << 0 << endl;
		return 0;
	}
	int i = 1;
	ll ans = inf;
	for(int ui = 0; ui < unq.size(); ui++) {
		while(i <= n and unq[ui].F > a[i]) i++;
		ll num = unq[ui].F;
		int knew = k - mp[a[i]];
		if(knew <= 0) {
			ans = 0;
			break;
		}

		ll opsRight, cr;
		int ni = unq[ui + 1].S;
		ll rnum = a[i] + 1;
		if(ui < unq.size() - 1) {
			opsRight = sfx[ni] - (rnum * (n - ni + 1));
			cr = n - ni + 1;
		} else {
			opsRight = 0;
			cr = 0;
		}

		ll opsLeft, cl;
		int li = i - 1;
		ll lnum = a[i] - 1;
		if(ui > 0) {
			opsLeft = (lnum * (li)) - pfx[li];
			cl = i - 1;
		} else {
			opsLeft = 0;
			cl = 0;
		}

		trace(i, opsRight, opsLeft, li, ni, lnum, rnum);

		if(cr >= knew) {
			ll candidate = opsRight + knew;
			trace(candidate);
			ans = min(candidate, ans);
		}

		if(cl >= knew) {
			ll candidate = opsLeft + knew;
			ans = min(ans, candidate);
		}

		if(ui > 0 and ui < unq.size() - 1) {
			ll candidate = opsLeft + opsRight + knew;
			ans = min(ans, candidate);
		}
	}
	cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,k,a[N];
LL s[N],ans=1e18,pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>k;
  for(int i=1;i<=n;i++)cin>>a[i];
  sort(a+1,a+1+n);
  for(int i=n;i>=1;i--)pre[i]=pre[i+1]+a[i];
  for(int i=1;i<=k;i++){
    s[i]=s[i-1]+a[i];
    LL tmp=-s[i]+1ll*i*a[i];
    int x=lower_bound(a+1,a+1+n,a[i]+1)-a;
    if(x-1>=k)ans=min(ans,tmp);
    else{
      LL y=pre[x]-1ll*a[x]*(n-x+1);
      ans=min(ans,tmp+y+1ll*(a[x]-a[i])*(k-x+1));
    }
  }
  cout<<ans<<'\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,k,a[N];
LL s[N],ans=1e18,pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>k;
  for(int i=1;i<=n;i++)cin>>a[i];
  sort(a+1,a+1+n);
  for(int i=n;i>=1;i--)pre[i]=pre[i+1]+a[i];
  for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
  for(int i=1;i<=n;i++){
    LL tmp=-s[i]+1ll*i*a[i];
    int x=lower_bound(a+1,a+1+n,a[i]+1)-a;
    if(x-1>=k)ans=min(ans,tmp);
    else{
      LL y=pre[x]-1ll*a[x]*(n-x+1);
      ans=min(ans,tmp+y+1ll*(a[x]-a[i])*(k-x+1));
    }
  }
  for(int i=n;i>=1;i--){
    LL tmp=pre[i]-1ll*(n-i+1)*a[i];
    int x=lower_bound(a+1,a+1+n,a[i])-a;
    x--;
    if(n-x>=k)ans=min(ans,tmp);
    else{
      LL y=1ll*x*a[x]-s[x];
      ans=min(ans,tmp+y+1ll*(a[i]-a[x])*(k-(n-i+1)));
    }
  }
  cout<<ans<<'\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,k,a[N];
LL s[N],ans=1e18,pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>k;
  for(int i=1;i<=n;i++)cin>>a[i];
  sort(a+1,a+1+n);
  for(int i=n;i>=1;i--)pre[i]=pre[i+1]+a[i];
  for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
  for(int i=1;i<=n;i++){
    LL tmp=-s[i]+1ll*i*a[i];
    int x=lower_bound(a+1,a+1+n,a[i]+1)-a;
    if(x-i>=k)return cout<<0,0;
    if(x-1>=k)ans=min(ans,tmp);
    else{
      LL y=pre[x]-1ll*a[x]*(n-x+1);
      ans=min(ans,tmp+y+1ll*(a[x]-a[i])*(k-x+1));
    }
  }
  for(int i=n;i>=1;i--){
    LL tmp=pre[i]-1ll*(n-i+1)*a[i];
    int x=lower_bound(a+1,a+1+n,a[i])-a;
    x--;
    if(n-x>=k)ans=min(ans,tmp);
    else{
      LL y=1ll*x*a[x]-s[x];
      ans=min(ans,tmp+y+1ll*(a[i]-a[x])*(k-(n-x)));
    }
  }
  cout<<ans<<'\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,k,a[N];
LL s[N],ans=1e18,pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>k;
  for(int i=1;i<=n;i++)cin>>a[i];
  sort(a+1,a+1+n);
  //for(int i=1;i<=n;i++)cout<<a[i]<<' ';cout<<'\n';
  for(int i=n;i>=1;i--)pre[i]=pre[i+1]+a[i];
  for(int i=1;i<=n;i++){
    s[i]=s[i-1]+a[i];
    if(i>=k&&a[i]==a[i-k+1])return cout<<0,0;
  }
  for(int i=1,j;i<=n;i=j){
    j=i;
    while(j+1<=n && a[j+1]==a[i])++j;
    int x=i-1,y=n-j,z=j-i+1;
   //cout<<i<<' '<<j<<' ';
    if(z+x>=k){
      ans=min(ans,-s[x]+1ll*x*a[i]-(x-(k-z)));
    }
    if(y+z>=k){
      ans=min(ans,pre[j+1]-1ll*y*a[j]-(y-(k-z)));
    }
    ans=min(ans,-s[x]+1ll*x*a[i]+pre[j+1]-1ll*y*a[j]-(x+y-(k-z)));
    ++j;//cout<<ans<<endl;
  }
  cout<<ans<<'\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,k,a[N];
LL s[N],ans=1e18,pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>k;
  for(int i=1;i<=n;i++)cin>>a[i];
  sort(a+1,a+1+n);
  for(int i=n;i>=1;i--)pre[i]=pre[i+1]+a[i];
  for(int i=1;i<=k;i++){
    s[i]=s[i-1]+a[i];
    LL tmp=-s[i]+1ll*i*a[i];
    int x=lower_bound(a+1,a+1+n,a[i]+1)-a;
    if(x-1>=k)ans=min(ans,tmp);
    else{
      LL y=pre[x]-1ll*a[x]*(n-x+1);
      ans=min(ans,tmp+y+(k-x+1));
      //cout<<i<<' '<<ans<<'\n';
    }
  }
  cout<<ans<<'\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,k,a[N];
LL s[N],ans=1e18,pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>k;
  for(int i=1;i<=n;i++)cin>>a[i];
  sort(a+1,a+1+n);
  for(int i=n;i>=1;i--)pre[i]=pre[i+1]+a[i];
  for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
  for(int i=1;i<=k;i++){
    LL tmp=-s[i]+1ll*i*a[i];
    int x=lower_bound(a+1,a+1+n,a[i]+1)-a;
    if(x-1>=k)ans=min(ans,tmp);
    else{
      LL y=pre[x]-1ll*a[x]*(n-x+1);
      ans=min(ans,tmp+y+1ll*(a[x]-a[i])*(k-x+1));
    }
  }
  for(int i=n;i>=n-k+1;i--){
    LL tmp=pre[i]-1ll*(n-i+1)*a[i];
    int x=lower_bound(a+1,a+1+n,a[i])-a;
    x--;
    if(n-x>=k)ans=min(ans,tmp);
    else{
      LL y=1ll*x*a[x]-s[x];
      ans=min(ans,tmp+y+1ll*(a[i]-a[x])*(k-(n-i+1)));
    }
  }
  cout<<ans<<'\n';
  return 0;
}


/*
 ********************************************************************************
 *  Author: ThoseBygones
 *  Version: V1.0
 *  Date: 2020-04-20
 *  Subject: ACM-ICPC
 *  Language: C/C++11
 *  OJ: CodeForces
 *  Algorithm: 枚举 + 前缀和 + 后缀和
 ********************************************************************************
 *  Algo-Description:
 ********************************************************************************
 */

#include <bits/stdc++.h>  //For CodeForces only
using namespace std;
template<class T> inline T sqr(T x)
{
    return x * x;
}
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u))
#define MAXN 200005

int a[MAXN];
LL pre[MAXN], suf[MAXN];   //前/后缀和

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++)
        pre[i] = pre[i-1] + a[i];
    for(int i = n; i; i--)
        suf[i] = suf[i+1] + a[i];
    LL ans = LINF;
    for(int i = 1; i <= n; i++) //枚举k个相同的数等于多少
    {
        if(i >= k)  //只调整小于a[i]的数
            ans = min(ans, 1LL * a[i] * i - pre[i] - (i - k));  //多于k个的数加到a[i]-1即可
        if(n - i + 1 >= k)  //只调整大于a[i]的数
            ans = min(ans, suf[i] - 1LL * a[i] * (n - i + 1) - (n - i + 1 - k));    //多于k个的数减到a[i]+1即可
        //都需要调整
        ans = min(ans, 1LL * a[i] * i - pre[i] + suf[i] - a[i] * (n - i + 1) - (n - k));    //多于k个的数少操作一次即可
    }
    printf("%lld\n", ans);
    return 0;
}


/*
 ********************************************************************************
 *  Author: ThoseBygones
 *  Version: V1.0
 *  Date: 2020-04-20
 *  Subject: ACM-ICPC
 *  Language: C/C++11
 *  OJ: CodeForces
 *  Algorithm: 枚举 + 前缀和 + 后缀和
 ********************************************************************************
 *  Algo-Description:
 ********************************************************************************
 */

#include <bits/stdc++.h>  //For CodeForces only
using namespace std;
template<class T> inline T sqr(T x)
{
    return x * x;
}
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u))
#define MAXN 200005

int a[MAXN];
LL pre[MAXN], suf[MAXN];   //前/后缀和

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++)
        pre[i] = pre[i-1] + a[i];
    for(int i = n; i; i--)
        suf[i] = suf[i+1] + a[i];
    int sum = 0;
    for(int i = 1; i <= n; i++)
    {
        if(a[i] == a[i-1])
            sum++;
        else
            sum = 1;
        if(sum >= k)
        {
            puts("0");
            return 0;s
        }
    }
    LL ans = LINF;
    for(int i = 1; i <= n; i++) //枚举k个相同的数等于多少
    {
        if(i >= k)  //只调整小于a[i]的数
            ans = min(ans, 1LL * a[i] * i - pre[i] - (i - k));  //多于k个的数加到a[i]-1即可
        if(n - i + 1 >= k)  //只调整大于a[i]的数
            ans = min(ans, suf[i] - 1LL * a[i] * (n - i + 1) - (n - i + 1 - k));    //多于k个的数减到a[i]+1即可
        //都需要调整
        ans = min(ans, 1LL * a[i] * i - pre[i] + suf[i] - 1LL * a[i] * (n - i + 1) - (n - k));    //多于k个的数少操作一次即可
    }
    printf("%lld\n", ans);
    return 0;
}


/*
 ********************************************************************************
 *  Author: ThoseBygones
 *  Version: V1.0
 *  Date: 2020-04-20
 *  Subject: ACM-ICPC
 *  Language: C/C++11
 *  OJ: CodeForces
 *  Algorithm: 枚举 + 前缀和 + 后缀和
 ********************************************************************************
 *  Algo-Description:
 ********************************************************************************
 */

#include <bits/stdc++.h>  //For CodeForces only
using namespace std;
template<class T> inline T sqr(T x)
{
    return x * x;
}
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u))
#define MAXN 200005

int a[MAXN];
LL pre[MAXN], suf[MAXN];   //前/后缀和

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++)
        pre[i] = pre[i-1] + a[i];
    for(int i = n; i; i--)
        suf[i] = suf[i+1] + a[i];
    LL ans = LINF;
    for(int i = 1; i <= n; i++) //枚举k个相同的数等于多少
    {
        if(i >= k)  //只调整小于a[i]的数
            ans = min(ans, 1LL * a[i] * i - pre[i] - (i - k));  //多于k个的数加到a[i]-1即可
        if(n - i + 1 >= k)  //只调整大于a[i]的数
            ans = min(ans, suf[i] - 1LL * a[i] * (n - i + 1) - (n - i + 1 - k));    //多于k个的数减到a[i]+1即可
        //都需要调整
        ans = min(ans, 1LL * a[i] * i - pre[i] + suf[i] - 1LL * a[i] * (n - i + 1) - (n - k));    //多于k个的数少操作一次即可
    }
    printf("%lld\n", ans);
    return 0;
}


/*
 ********************************************************************************
 *  Author: ThoseBygones
 *  Version: V1.0
 *  Date: 2020-04-20
 *  Subject: ACM-ICPC
 *  Language: C/C++11
 *  OJ: CodeForces
 *  Algorithm: 枚举 + 前缀和 + 后缀和
 ********************************************************************************
 *  Algo-Description:
 ********************************************************************************
 */

#include <bits/stdc++.h>  //For CodeForces only
using namespace std;
template<class T> inline T sqr(T x)
{
    return x * x;
}
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u))
#define MAXN 200005

int a[MAXN];
LL pre[MAXN], suf[MAXN];   //前/后缀和

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++)
        pre[i] = pre[i-1] + a[i];
    for(int i = n; i; i--)
        suf[i] = suf[i+1] + a[i];
    int sum = 0;
    for(int i = 1; i <= n; i++)
    {
        if(a[i] == a[i-1])
            sum++;
        else
            sum = 1;
        if(sum >= k)
        {
            puts("0");
            return 0;
        }
    }
    LL ans = LINF;
    for(int i = 1; i <= n; i++) //枚举k个相同的数等于多少
    {
        if(i >= k)  //只调整小于a[i]的数
            ans = min(ans, 1LL * a[i] * i - pre[i] - (i - k));  //多于k个的数加到a[i]-1即可
        if(n - i + 1 >= k)  //只调整大于a[i]的数
            ans = min(ans, suf[i] - 1LL * a[i] * (n - i + 1) - (n - i + 1 - k));    //多于k个的数减到a[i]+1即可
        //都需要调整
        ans = min(ans, 1LL * a[i] * i - pre[i] + suf[i] - 1LL * a[i] * (n - i + 1) - (n - k));    //多于k个的数少操作一次即可
    }
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             if(i==n-1)
             {v.push_back(make_pair(arr[i],p));continue;}
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=req+req2;
             mx=min(mx,temp);

         }
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=req+req2;
             mx=min(mx,temp);

         }
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             if(i==n-1)
             {v.push_back(make_pair(arr[i],p));continue;}
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=max(ll(0),req+req2);
             mx=min(mx,temp);

         }
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             if(i==n-1)
             {v.push_back(make_pair(arr[i],p));continue;}
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=max((ll)0,req+req2);
             mx=min(mx,temp);

         }
         if(k==1)
         mx=0;
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             if(i==n-1)
             {v.push_back(make_pair(arr[i],p));continue;}
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=max((ll)0,req+req2);
             mx=min(mx,temp);

         }
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll arr[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    vector<pair<ll,ll>>v;
    ll flag=1;
    for(ll i=0;i<n-1;i++)
    {
        ll p=1;
        while(arr[i]==arr[i+1])
        {
            p++;
            if(i==n-2)
            {
                flag=0;
                break;
            }
            i++;
        }
        v.push_back(make_pair(arr[i],p));
    }
    if(flag==1)
    {
        v.push_back(make_pair(arr[n-1],1));
    }

    ll prefcnt[n+1];
    ll prefsm[n+1];
    ll sm=0,cnt=0;
    for(ll i=0;i<v.size();i++)
    {
        cnt+=v[i].second;
        sm+=(v[i].second*v[i].first);
        prefsm[i]=sm;
        prefcnt[i]=cnt;
    }
    ll ans=100000000000000;
    for(ll i=0;i<v.size();i++)
    {
        ll need,needl=0,needr=0,req=0,req2=0;
        need=max((ll)0,k-v[i].second);
        //cout<<"need="<<need<<" ";
        if(i!=0)
        {
            if(need!=0)
            {
                //cout<<"prefcnt="<<prefcnt[i-1]<<" ";
                needl=min(need,prefcnt[i-1]);
                if(needl!=0)
                {
                    req=(v[i].first-1)*prefcnt[i-1]-prefsm[i-1]+needl;
                }
            }
        }
        //cout<<"req="<<req<<" ";
        if(i!=(n-1))
        {
            if(need!=0)
            {
                needr=max((ll)0,need-needl);
                if(needr!=0)
                {    // cout<<"needr="<<needr<<" ";
                ll a=prefsm[v.size()-1]-prefsm[i];
                ll b=(prefcnt[v.size()-1]-prefcnt[i]);
                //cout<<"a="<<a<<" "<<"b="<<b<<" ";
                    req2=a-(v[i].first+1)*b+needr;
                }
            }
        }
        //cout<<"req2="<<req2<<" ";
        ll tmp=req+req2;
        //cout<<tmp<<endl;
        ll needll=0,needrr=0,reqq=0,reqq2=0;
                if(i!=(n-1))
        {
            if(need!=0)
            {
                needrr=min(need,prefcnt[v.size()-1]-prefcnt[i]);
                if(needrr!=0)
                {    // cout<<"needr="<<needr<<" ";
                ll a=prefsm[v.size()-1]-prefsm[i];
                ll b=(prefcnt[v.size()-1]-prefcnt[i]);
                //cout<<"a="<<a<<" "<<"b="<<b<<" ";
                    reqq2=a-(v[i].first+1)*b+needrr;
                }
            }
        }
        if(i!=0)
        {
            if(need!=0)
            {
                //cout<<"prefcnt="<<prefcnt[i-1]<<" ";
                needll=max((ll)0,need-needrr);
                if(needll!=0)
                {
                    reqq=(v[i].first-1)*prefcnt[i-1]-prefsm[i-1]+needll;
                }
            }
        }
        ll temp=reqq+reqq2;
        ll mn=min(temp,tmp);
        ans=min(ans,mn);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             if(i==n-1)
             {v.push_back(make_pair(arr[i],p));continue;}
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=max(ll(0),req+req2);
             mx=min(mx,temp);

         }
         if(k==1)
         mx=0;
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll arr[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    vector<pair<ll,ll>>v;
    ll flag=1;
    for(ll i=0;i<n-1;i++)
    {
        ll p=1;
        while(arr[i]==arr[i+1])
        {
            p++;
            if(i==n-2)
            {
                flag=0;
                break;
            }
            i++;
        }
        v.push_back(make_pair(arr[i],p));
    }
    if(flag==1)
    {
        v.push_back(make_pair(arr[n-1],1));
    }

    ll prefcnt[n+1];
    ll prefsm[n+1];
    ll sm=0,cnt=0;
    for(ll i=0;i<v.size();i++)
    {
        cnt+=v[i].second;
        sm+=(v[i].second*v[i].first);
        prefsm[i]=sm;
        prefcnt[i]=cnt;
    }
    ll ans=100000000000000;
    for(ll i=0;i<v.size();i++)
    {
        ll need,needl=0,needr=0,req=0,req2=0;
        need=max((ll)0,k-v[i].second);
        //cout<<"need="<<need<<" ";
        if(i!=0)
        {
            if(need!=0)
            {
                needl=min(need,prefcnt[i-1]);
                if(needl!=0)
                {
                    req=(v[i].first-1)*prefcnt[i-1]-prefsm[i-1]+needl;
                }
            }
        }
        //cout<<"req="<<req<<" ";
        if(i!=(n-1))
        {
            if(need!=0)
            {
                needr=max((ll)0,need-needl);
                if(needr!=0)
                {    // cout<<"needr="<<needr<<" ";
                ll a=prefsm[v.size()-1]-prefsm[i];
                ll b=(prefcnt[v.size()-1]-prefcnt[i]);
                //cout<<"a="<<a<<" "<<"b="<<b<<" ";
                    req2=a-(v[i].first+1)*b+needr;
                }
            }
        }
        //cout<<"req2="<<req2<<" ";
        ll tmp=req+req2;
        //cout<<tmp<<endl;
        ans=min(ans,tmp);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
ll pow_mod(ll a,ll b)
{
    ll res=1;
    while(b!=0)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b/=2;
    }
    return res;
}
int main()
{

         ll n,k;
         cin>>n>>k;
         ll arr[n];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
         }
         sort(arr,arr+n);
         vector<pair<ll,ll>>v;
         for(ll i=0;i<n;i++)
         {
             ll p=1;
             if(i==n-1)
             {v.push_back(make_pair(arr[i],p));continue;}
             while(arr[i]==arr[i+1])
             {
                 p++;
                 if(i==n-2)
                 break;
                 i++;
             }
             v.push_back(make_pair(arr[i],p));
         }
         sort(v.begin(),v.end());
         ll sm=0;
         ll cnt=0;
         ll prefcnt[v.size()];
         ll presum[v.size()];
         for(ll i=0;i<v.size();i++)
         {
             cnt+=v[i].second;
             sm+=(v[i].second*v[i].first);
             prefcnt[i]=cnt;
             presum[i]=sm;
         }
         ll mx=900000000000000;
         for(ll i=0;i<v.size();i++)
         {

             ll req=0,req2=0,needl=0,need=0;

               if(k>v[i].second);
               need=k-v[i].second;
               if(i!=0)
               {
             needl=min(need,prefcnt[i-1]);
             if(needl!=0)
             req=(v[i].first-1)*prefcnt[i-1]-presum[i-1]+needl;
             }
             if(i!=n-1)
             {
             ll needr=max((ll)0,need-needl);
             if(needr!=0)
             req2=presum[v.size()-1]-presum[i]-(v[i].first+1)*(prefcnt[v.size()-1]-prefcnt[i])+needr;

             }
             ll temp=max((ll)0,req+req2);
             mx=min(mx,temp);

         }
         if(k==1)
         mx=0;
        cout<<mx<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ll n,k;
    cin>>n>>k;
    ll arr[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    vector<pair<ll,ll>>v;
    ll flag=1;
    for(ll i=0;i<n-1;i++)
    {
        ll p=1;
        while(arr[i]==arr[i+1])
        {
            p++;
            if(i==n-2)
            {
                flag=0;
                break;
            }
            i++;
        }
        v.push_back(make_pair(arr[i],p));
    }
    if(flag==1)
    {
        v.push_back(make_pair(arr[n-1],1));
    }

    ll prefcnt[n+1];
    ll prefsm[n+1];
    ll sm=0,cnt=0;
    for(ll i=0;i<v.size();i++)
    {
        cnt+=v[i].second;
        sm+=(v[i].second*v[i].first);
        prefsm[i]=sm;
        prefcnt[i]=cnt;
    }
    ll ans=100000000000000;
    for(ll i=0;i<v.size();i++)
    {
        ll need,needl=0,needr=0,req=0,req2=0;
        need=max((ll)0,k-v[i].second);
        //cout<<"need="<<need<<" ";
        if(i!=0)
        {
            if(need!=0)
            {
                needl=min(need,prefcnt[i-1]);
                if(needl!=0)
                {
                    req=(v[i].first-1)*prefcnt[i-1]-prefsm[i-1]+needl;
                }
            }
        }
        cout<<"req="<<req<<" ";
        if(i!=(n-1))
        {
            if(need!=0)
            {
                needr=max((ll)0,need-needl);
                if(needr!=0)
                {    // cout<<"needr="<<needr<<" ";
                ll a=prefsm[v.size()-1]-prefsm[i];
                ll b=(prefcnt[v.size()-1]-prefcnt[i]);
                //cout<<"a="<<a<<" "<<"b="<<b<<" ";
                    req2=a-(v[i].first+1)*b+needr;
                }
            }
        }
        //cout<<"req2="<<req2<<" ";
        ll tmp=req+req2;
        //cout<<tmp<<endl;
        ans=min(ans,tmp);
    }
    cout<<ans<<endl;
    return 0;
}


//problem:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

namespace Fread{
const int MAXN=1<<20;
char buf[MAXN],*S,*T;
inline char getchar(){
	if(S==T){
		T=(S=buf)+fread(buf,1,MAXN,stdin);
		if(S==T)return EOF;
	}
	return *S++;
}
}//namespace Fread
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
inline int read(){
	int f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll readll(){
	ll f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
/*  ------  by:duyi  ------  */ // myt天下第一
const int MAXN=2e5;
const ll INF=1e18;
int n,K,a[MAXN+5];
ll pre[MAXN+5],suf[MAXN+5];
int main() {
	n=read();K=read();
	for(int i=1;i<=n;++i)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)pre[i]=pre[i-1]+a[i];
	for(int i=n;i>=1;--i)suf[i]=suf[i+1]+a[i];
	ll ans=INF;
	for(int i=1;i<=n;++i){
		int j=i;
		while(j+1<=n&&a[j+1]==a[i])++j;
		if(j-i+1>=K){ans=0;break;}
		ans=min(ans,(ll)(a[i]-1)*(i-1)-pre[i-1]+suf[j+1]-(ll)(a[i]+1)*(n-j)+K-(j-i+1));
		if(j>=K){
			ans=min(ans,(ll)(a[i]-1)*(i-1)-pre[i-1]+K-(j-i+1));
		}
		if(n-i+1>=K){
			ans=min(ans,suf[j+1]-(ll)(a[i]+1)*(n-j)+K-(j-i+1));
		}
		i=j;
	}
	cout<<ans<<endl;
	return 0;
}


//problem:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

namespace Fread{
const int MAXN=1<<20;
char buf[MAXN],*S,*T;
inline char getchar(){
	if(S==T){
		T=(S=buf)+fread(buf,1,MAXN,stdin);
		if(S==T)return EOF;
	}
	return *S++;
}
}//namespace Fread
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
inline int read(){
	int f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll readll(){
	ll f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
/*  ------  by:duyi  ------  */ // myt天下第一
const int MAXN=2e5;
const ll INF=1e18;
int n,K,a[MAXN+5];
ll pre[MAXN+5],suf[MAXN+5];
int main() {
	n=read();K=read();
	for(int i=1;i<=n;++i)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)pre[i]=pre[i-1]+a[i];
	for(int i=n;i>=1;--i)suf[i]=suf[i+1]+a[i];
	ll ans=INF;
	for(int i=1;i<=n;++i){
		int j=i;
		while(j+1<=n&&a[j+1]==a[i])++j;
		ans=min(ans,(ll)(a[i]-1)*(i-1)-pre[i-1]+suf[i]-(ll)(a[i]+1)*(n-i)+K-(j-i+1));
		i=j;
	}
	cout<<ans<<endl;
	return 0;
}


//problem:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

namespace Fread{
const int MAXN=1<<20;
char buf[MAXN],*S,*T;
inline char getchar(){
	if(S==T){
		T=(S=buf)+fread(buf,1,MAXN,stdin);
		if(S==T)return EOF;
	}
	return *S++;
}
}//namespace Fread
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
inline int read(){
	int f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll readll(){
	ll f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
/*  ------  by:duyi  ------  */ // myt天下第一
const int MAXN=2e5;
const ll INF=1e18;
int n,K,a[MAXN+5];
ll pre[MAXN+5],suf[MAXN+5];
int main() {
	n=read();K=read();
	for(int i=1;i<=n;++i)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)pre[i]=pre[i-1]+a[i];
	for(int i=n;i>=1;--i)suf[i]=suf[i+1]+a[i];
	ll ans=INF;
	for(int i=1;i<=n;++i){
		int j=i;
		while(j+1<=n&&a[j+1]==a[i])++j;
		ans=min(ans,(ll)(a[i]-1)*(i-1)-pre[i-1]+suf[i+1]-(ll)(a[i]+1)*(n-i)+K-(j-i+1));
		i=j;
	}
	cout<<ans<<endl;
	return 0;
}


//problem:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

namespace Fread{
const int MAXN=1<<20;
char buf[MAXN],*S,*T;
inline char getchar(){
	if(S==T){
		T=(S=buf)+fread(buf,1,MAXN,stdin);
		if(S==T)return EOF;
	}
	return *S++;
}
}//namespace Fread
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
inline int read(){
	int f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll readll(){
	ll f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
/*  ------  by:duyi  ------  */ // myt天下第一
const int MAXN=2e5;
const ll INF=1e18;
int n,K,a[MAXN+5];
ll pre[MAXN+5],suf[MAXN+5];
int main() {
	n=read();K=read();
	for(int i=1;i<=n;++i)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)pre[i]=pre[i-1]+a[i];
	for(int i=n;i>=1;--i)suf[i]=suf[i+1]+a[i];
	ll ans=INF;
	for(int i=1;i<=n;++i){
		int j=i;
		while(j+1<=n&&a[j+1]==a[i])++j;
		ans=min(ans,(ll)(a[i]-1)*(i-1)-pre[i-1]+suf[j+1]-(ll)(a[i]+1)*(n-j)+K-(j-i+1));
		i=j;
	}
	cout<<ans<<endl;
	return 0;
}


//problem:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

namespace Fread{
const int MAXN=1<<20;
char buf[MAXN],*S,*T;
inline char getchar(){
	if(S==T){
		T=(S=buf)+fread(buf,1,MAXN,stdin);
		if(S==T)return EOF;
	}
	return *S++;
}
}//namespace Fread
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
inline int read(){
	int f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll readll(){
	ll f=1,x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
/*  ------  by:duyi  ------  */ // myt天下第一
const int MAXN=2e5;
const ll INF=1e18;
int n,K,a[MAXN+5];
ll pre[MAXN+5],suf[MAXN+5];
int main() {
	n=read();K=read();
	for(int i=1;i<=n;++i)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)pre[i]=pre[i-1]+a[i];
	for(int i=n;i>=1;--i)suf[i]=suf[i+1]+a[i];
	ll ans=INF;
	for(int i=1;i<=n;++i){
		int j=i;
		while(j+1<=n&&a[j+1]==a[i])++j;
		if(j-i+1>=K){ans=0;break;}
		ans=min(ans,(ll)(a[i]-1)*(i-1)-pre[i-1]+suf[j+1]-(ll)(a[i]+1)*(n-j)+K-(j-i+1));
		i=j;
	}
	cout<<ans<<endl;
	return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(i==500)
            cout<<(a[i]*i-b[i])<<endl;
    }
    cout<<minn<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int a[N],b[N],c[N];
ll lsum[N],rsum[N];
int lc[N],rc[N];
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    int cnt=unique(b+1,b+1+n)-b-1;
    int maxx=0;
    b[0]=b[1];
    b[cnt+1]=b[cnt];
    for(int i=1;i<=n;i++){
        int tmp=lower_bound(b+1,b+1+cnt,a[i])-b;
        c[tmp]++;
        maxx=max(c[tmp],maxx);
    }
    if(maxx>=k){
        puts("0");
        return 0;
    }
    ll l=0,r=0;
    int tmp=0;
    for(int i=1;i<=cnt;i++){
        lsum[i]=lsum[i-1];
        if(tmp+c[i]>=k){
            l=lsum[i]+1ll*(k-c[i])*(b[i]-b[i-1]);
            lsum[i]+=1ll*tmp*(b[i]-b[i-1]);
            tmp+=c[i];
            lc[i]=tmp;
            break;
        }
        lsum[i]+=1ll*tmp*(b[i]-b[i-1]);
        tmp+=c[i];
        lc[i]=tmp;
    }
    tmp=0;
    for(int i=cnt;i>=1;i--){
        rsum[i]=rsum[i+1];
        if(tmp+c[i]>=k){
            r=rsum[i]+1ll*(k-c[i])*(b[i+1]-b[i]);
            rsum[i]+=1ll*tmp*(b[i+1]-b[i]);
            tmp+=c[i];
            rc[i]=tmp;
            break;
        }
        rsum[i]+=1ll*tmp*(b[i+1]-b[i]);
        tmp+=c[i];
        rc[i]=tmp;
    }
    /*
    for(int i=1;i<=cnt;i++){
        cout<<i<<' '<<lsum[i]<<' '<<rsum[i]<<endl;
    }
    return 0;
    */
    for(int i=1;i<=cnt;i++){
        if(!lc[i])
            break;
        if(rc[i]){
            if(k-lc[i]>0)
                l=min(l,lsum[i]+rsum[i]-rc[i+1]+k-lc[i]);
        }
    }
    for(int i=cnt;i>=1;i--){
        if(!rc[i])
            break;
        if(lc[i]){
            if(k-rc[i]>0)
                r=min(r,rsum[i]+lsum[i]-lc[i-1]+k-rc[i]);
        }
    }

    printf("%lld\n",min(l,r));
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    int t1=a[1],t2=a[2];
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(i==500 && t1==561987393 && t2==561987488)
            cout<<(a[i]*i-b[i])<<endl;
    }
    cout<<minn<<endl;
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    int t1=a[1],t2=a[2];
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(i==500 && t1==561987393 && t2==561987488){
            cout<<(a[i]*i-b[i])<<endl;
            cout<<(b[N]-b[i+1])-(N-i-1)*a[i+1]<<endl;
            cout<<(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K<<endl;
        }

    }
    cout<<minn<<endl;
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(minn=253245)
            cout<<i<<endl;
    }
    cout<<minn<<endl;
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(minn==253245)
            cout<<i<<endl;
    }
    cout<<minn<<endl;
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(i==500 && a[1]==561987393 && a[2]==561987488)
            cout<<(a[i]*i-b[i])<<endl;
    }
    cout<<minn<<endl;
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    int t1=a[1],t2=a[2];
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(i==500 && t1==561987393 && t2==561987488){
            cout<<(a[i]*i-b[i])<<endl;
            cout<<(b[N]-b[i+1])-(N-i-1)*a[i+1]<<endl;
        }

    }
    cout<<minn<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int a[N],b[N],c[N];
ll lsum[N],rsum[N];
int lc[N],rc[N];
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    int cnt=unique(b+1,b+1+n)-b-1;
    int maxx=0;
    b[0]=b[1];
    b[cnt+1]=b[cnt];
    for(int i=1;i<=n;i++){
        int tmp=lower_bound(b+1,b+1+cnt,a[i])-b;
        c[tmp]++;
        maxx=max(c[tmp],maxx);
    }
    if(maxx>=k){
        puts("0");
        return 0;
    }
    ll l=0,r=0;
    int tmp=lc[1]=c[1];
    for(int i=2;i<=cnt;i++){
        lsum[i]=lsum[i-1];
        if(tmp+c[i]>=k){
            lsum[i]+=1ll*(k-c[i])*(b[i]-b[i-1]);
            lc[i]=k;
            l=lsum[i];
            break;
        }
        lsum[i]+=1ll*tmp*(b[i]-b[i-1]);
        tmp+=c[i];
        lc[i]=tmp;
    }
    tmp=rc[cnt]=c[cnt];
    for(int i=cnt-1;i>=1;i--){
        rsum[i]=rsum[i+1];
        if(tmp+c[i]>=k){
            rsum[i]+=1ll*(k-c[i])*(b[i+1]-b[i]);
            r=rsum[i];
            rc[i]=k;
            break;
        }
        rsum[i]+=1ll*tmp*(b[i+1]-b[i]);
        tmp+=c[i];
        rc[i]=tmp;
    }
    /*
    if(a[1]==561987393 && a[2]==561987488){
        cout<<b[309]<<' '<<b[310]<<' '<<b[311]<<' '<<b[312]<<endl;
        cout<<lc[309]<<' '<<lsum[309]<<endl;
        cout<<lc[310]<<' '<<lsum[310]<<endl;
        cout<<rc[311]<<' '<<rsum[311]<<endl;
        cout<<rc[312]<<' '<<rsum[312]<<endl;
    }*/
    for(int i=1;i<=cnt;i++){
        if(!lc[i])
            break;
        if(rc[i]){
            l=min(l,lsum[i]+rsum[i+1]+1ll*(k-lc[i])*(b[i+1]-b[i]));
        }
    }
    for(int i=cnt;i>=1;i--){
        if(!rc[i])
            break;
        if(lc[i]){
            r=min(r,rsum[i]+lsum[i-1]+1ll*(k-rc[i])*(b[i]-b[i-1]));
        }
    }

    printf("%lld\n",min(l,r));
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
    }
    cout<<minn<<endl;
    return 0;
}


#include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll;
ll a[MAXN],b[MAXN];
int main(){
    int N,K,flag=0;
    map<ll,int>m;
    cin>>N>>K;
    for(int i=1;i<=N;++i){
        cin>>a[i];
        m[a[i]]++;
        if(m[a[i]]>=K)flag=1;
    }
    int t1=a[1],t2=a[2];
    if(flag){
        cout<<0<<endl;
        return 0;
    }
    sort(a+1,a+1+N);
    for(int i=1;i<=N;++i)
        b[i]=b[i-1]+a[i];
    ll minn=1e16;
    for(int i=1;i<=N;++i){
        minn=min(minn,(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K);
        if(i>=K)minn=min(minn,(a[i]*i-b[i])-i+K);
        if(i+K<=N+1)minn=min(minn,(b[N]-b[i-1])-(N-i+1)*a[i]-(N-i+1)+K);
        if(i==500 && t1==561987393 && t2==561987488){
            cout<<(a[i]*i-b[i])<<endl;
            cout<<(b[N]-b[i+1])-(N-i-1)*a[i+1]<<endl;
            cout<<(a[i]*i-b[i])+(b[N]-b[i])-(N-i)*a[i]-N+K)<<endl;
        }

    }
    cout<<minn<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int a[N],b[N],c[N];
ll lsum[N],rsum[N];
int lc[N],rc[N];
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    int cnt=unique(b+1,b+1+n)-b-1;
    int maxx=0;
    b[0]=b[1];
    b[cnt+1]=b[cnt];
    for(int i=1;i<=n;i++){
        int tmp=lower_bound(b+1,b+1+cnt,a[i])-b;
        c[tmp]++;
        maxx=max(c[tmp],maxx);
    }

    /*
    for(int i=1;i<=cnt;i++){
        cout<<i<<' '<<c[i]<<endl;
    }
    return 0;*/
    if(maxx>=k){
        puts("0");
        return 0;
    }
    ll l=0,r=0;
    int tmp=0;
    for(int i=1;i<=cnt;i++){
        lsum[i]=lsum[i-1];
        if(tmp+c[i]>=k){
            lsum[i]+=1ll*(k-c[i])*(b[i]-b[i-1]);
            lc[i]=k;
            l=lsum[i];
            //cout<<lc[i]<<' '<<lsum[i]<<endl;
            break;
        }
        lsum[i]+=1ll*tmp*(b[i]-b[i-1]);
        tmp+=c[i];
        lc[i]=tmp;
        //cout<<lc[i]<<' '<<lsum[i]<<endl;
    }
    tmp=0;
    for(int i=cnt;i>=1;i--){
        rsum[i]=rsum[i+1];
        if(tmp+c[i]>=k){
            rsum[i]+=1ll*(k-c[i])*(b[i+1]-b[i]);
            r=rsum[i];
            rc[i]=k;
           // cout<<rc[i]<<' '<<rsum[i]<<endl;
            break;
        }
        rsum[i]+=1ll*tmp*(b[i+1]-b[i]);
        tmp+=c[i];
        rc[i]=tmp;
        //cout<<rc[i]<<' '<<rsum[i]<<endl;
    }
    if(a[1]==561987393 && a[2]==561987488){
        cout<<b[309]<<' '<<b[310]<<' '<<b[311]<<' '<<b[312]<<endl;
        cout<<lc[309]<<' '<<lsum[309]<<endl;
        cout<<lc[310]<<' '<<lsum[310]<<endl;
        cout<<rc[311]<<' '<<rsum[311]<<endl;
        cout<<rc[312]<<' '<<rsum[312]<<endl;
    }
    for(int i=1;i<=cnt;i++){
        if(!lc[i])
            break;
        if(rc[i]){
            l=min(l,lsum[i]+rsum[i+1]+1ll*(k-lc[i])*(b[i+1]-b[i]));
        }
    }
    for(int i=cnt;i>=1;i--){
        if(!rc[i])
            break;
        if(lc[i]){
            r=min(r,rsum[i]+lsum[i-1]+1ll*(k-rc[i])*(b[i]-b[i-1]));
        }
    }

    printf("%lld\n",min(l,r));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn=2e5+5;
long long a[maxn],b[maxn];
long long cnt1[maxn],cnt2[maxn],cnt[maxn];
long long pre[maxn],suf[maxn];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n,K;
    cin>>n>>K;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[++b[0]]=a[i];
    }
    sort(b+1,b+b[0]+1);
    b[0]=unique(b+1,b+b[0]+1)-b-1;
    for(int i=1;i<=n;i++){
        int p=lower_bound(b+1,b+b[0]+1,a[i])-b;
        cnt[p]++;
    }
    for(int i=1;i<=b[0];i++){
        pre[i]=pre[i-1]+cnt[i]*b[i];
        cnt1[i]=cnt1[i-1]+cnt[i];
    }
    for(int i=b[0];i>=1;i--){
        suf[i]=suf[i+1]+cnt[i]*b[i];
        cnt2[i]=cnt2[i+1]+cnt[i];
    }
    long long res=0x3f3f3f3f3f3f3f3f;
    for(int i=1;i<=b[0];i++){
        int k=K-cnt[i];
        if(k<=0) res=0;

        long long t1,t2;
        t1=cnt1[i-1]*b[i]-pre[i-1]-cnt1[i-1]+k;
        t2=suf[i+1]-cnt2[i+1]*b[i]-cnt2[i+1]+k;
        if(cnt1[i-1]>=k) res=min(res,t1);
        else if(cnt2[i+1]>=k) res=min(res,t2);
        else{
            long long temp=b[i]*cnt1[i-1]-pre[i-1]+t2-cnt1[i-1];
            res=min(res,temp);
            temp=suf[i+1]-b[i]*cnt2[i+1]+t1-cnt2[i+1];
            res=min(res,temp);
        }
    }
    cout<<max(0ll,res)<<endl;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn=2e5+5;
long long a[maxn],b[maxn];
long long cnt1[maxn],cnt2[maxn],cnt[maxn];
long long pre[maxn],suf[maxn];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n,K;
    cin>>n>>K;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[++b[0]]=a[i];
    }
    sort(b+1,b+b[0]+1);
    b[0]=unique(b+1,b+b[0]+1)-b-1;
    for(int i=1;i<=n;i++){
        int p=lower_bound(b+1,b+b[0]+1,a[i])-b;
        cnt[p]++;
    }
    for(int i=1;i<=b[0];i++){
        pre[i]=pre[i-1]+cnt[i]*b[i];
        cnt1[i]=cnt1[i-1]+cnt[i];
    }
    for(int i=b[0];i>=1;i--){
        suf[i]=suf[i+1]+cnt[i]*b[i];
        cnt2[i]=cnt2[i+1]+cnt[i];
    }
    long long res=0x3f3f3f3f3f3f3f3f;
    for(int i=1;i<=b[0];i++){
        int k=K-cnt[i];
        if(k<=0) res=0;

        long long t1,t2;
        t1=cnt1[i-1]*b[i]-pre[i-1]-cnt1[i-1]+k;
        t2=suf[i+1]-cnt2[i+1]*b[i]-cnt2[i+1]+k;
        if(cnt1[i-1]>=k) res=min(res,t1);
        else if(cnt2[i+1]>=k) res=min(res,t2);
        else{
            long long temp=b[i]*cnt1[i-1]-pre[i-1]+t2-cnt1[i-1];
            res=min(res,temp);
            temp=suf[i+1]-b[i]*cnt2[i+1]+t1-cnt2[i+1];
            res=min(res,temp);
        }
    }
    cout<<res<<endl;
}