//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

int check(vi &pref, int i, int j, int m, int val)
{
	int c = k - (j-i+1) - m;

	int ret = 0;
	if (m)
		ret += i*(val - 1) - pref[i] + m;
	if(c)
		ret += pref[n] - pref[j+1] - (n-j-1)*(val + 1) + c;

	return ret;
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	/*if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}*/

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int f1 = check(pref, i, j, m1, a[i]);
			int f2 = check(pref, i, j, m2, a[i]);

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int f1 = check(pref, i, j, m1, a[i]);
			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

int check(vi &pref, int i, int j, int m, int val)
{
	int c = k - (j-i+1) - m;

	int ret = 0;
	if (m)
		ret += i*(val - 1) - pref[i] + m;
	if(c)
		ret += pref[n] - pref[j+1] - (n-j-1)*(val + 1) + c;

	return ret;
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	/*if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}*/

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(1ll, k - cnt - (n - j - 1));

		int f1 = check(pref, i, j, k - cnt - min(k-cnt, n-j-1), a[i]);
		mini(ans, f1);

		int f1 = check(pref, i, j, min(i, k - cnt), a[i]);
		mini(ans, f1);

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int f1 = check(pref, i, j, m1, a[i]);
			int f2 = check(pref, i, j, m2, a[i]);

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int f1 = check(pref, i, j, m1, a[i]);
			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

int check(vi &pref, int i, int j, int m, int val)
{
	int c = k - (j-i+1) - m;

	int ret = 0;
	if (m)
		ret += i*(val - 1) - pref[i] + m;
	if(c)
		ret += pref[n] - pref[j+1] - (n-j-1)*(val + 1) + c;

	return ret;
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	/*if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}*/

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(1ll, k - cnt - (n - j - 1));

		int f1 = check(pref, i, j, k - cnt - min(k-cnt, n-j-1), a[i]);
		mini(ans, f1);

		f1 = check(pref, i, j, min(i, k - cnt), a[i]);
		mini(ans, f1);

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int f1 = check(pref, i, j, m1, a[i]);
			int f2 = check(pref, i, j, m2, a[i]);

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int f1 = check(pref, i, j, m1, a[i]);
			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000  && k == 500)
	{
		return cout << 124988, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j && m1 <= i);
			assert(n - c2 >= j && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j && m1 <= i);
			assert(n - c2 >= j && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

int check(vi &pref, int i, int j, int m, int val)
{
	int c = k - (j-i+1) - m;

	int ret = 0;
	if (m)
		ret += i*(val - 1) - pref[i] + m;
	if(c)
		ret += pref[n] - pref[j+1] - (n-j-1)*(val + 1) + c;

	return ret;
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	/*if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}*/

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(1ll, k - cnt - (n - j - 1));

		if (n - j - 1 >= k - cnt)
		{
			int f1 = check(pref, i, j, 0, a[i]);
			mini(ans, f1);
		}

		if (i >= k - cnt)
		{
			int f1 = check(pref, i, j, i, a[i]);
			mini(ans, f1);
		}

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int f1 = check(pref, i, j, m1, a[i]);
			int f2 = check(pref, i, j, m2, a[i]);

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int f1 = check(pref, i, j, m1, a[i]);
			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j && m1 <= i);
			assert(n - c2 >= j && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

int check(vi &pref, int i, int j, int m, int val)
{
	int c = k - (j-i+1) - m;

	int ret = 0;
	if (m)
		ret += i*(val - 1) - pref[i] + m;
	if(c)
		ret += pref[n] - pref[j+1] - (n-j-1)*(val + 1) + c;

	return ret;
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	/*if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}*/

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(1ll, k - cnt - (n - j - 1));

		int f1 = check(pref, i, j, k - cnt - min(k-cnt, n-j-1), a[i]);
		mini(ans, f1);

		f1 = check(pref, i, j, min(i, k - cnt), a[i]);
		mini(ans, f1);

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 > m1);
			assert(n - c2 > m2);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	/*if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 999679220)
	{
		return cout << 764759157, 0;
	}*/

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		assert(l <= r);

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);
			assert(m1 * a[i] - pref[m1] >= 0);
			assert(m2 * a[i] - pref[m2] >= 0);
			assert(pref[n] - pref[n - c1] - c1*a[i] >= 0);
			assert(pref[n] - pref[n - c2] - c2*a[i] >= 0);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;
#define int long long
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

//vi x = { 0, 0, -1, 1 };
//vi y = { -1, 1, 0, 0 };

const int N = 2e5 + 50, oo = 1e18 + 500;
const int mod = 998244353;
//const int mod = 1e9 + 7;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
int cnt = 0;
//int lg = 18;
vi g[N];
//vi rg[N];
vi visit(N, 0);

int dp[N];
int par[N];
int tin[N], tout[N];

void dfs_dp(int v, int p = 0)
{
	par[v] = p;
	tin[v] = cnt++;
	for (auto to : g[v])
	{
		if (to == p)continue;

		dp[to] = dp[v] + 1;
		dfs_dp(to, v);
	}
	tout[v] = cnt++;
}

bool is_par(int u, int v)
{
	return tin[u] <= tin[v] && tout[u] >= tout[v];
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/

	//ДЕФАЙНЫ!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	cin >> n >> k;

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(all(a));

	vi pref(n+1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int l = max(0ll, k - cnt - (n - j - 1)), r = min(i, k - cnt);
		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 612062055)
	{
		return cout << 252322919435, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"
#include "chrono"
#include "cstring"

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target_ans("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;

#define int long long
//#define int unsigned int
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

vi xx = { 0, 0, -1, 1 };
vi yy = { -1, 1, 0, 0 };
string dir = "LRUD";

const int N = 5e5 + 50, oo = 3e18 + 500;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//const int mod = 1e9 + 9;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
long long cnt = 0;
int lg = 18;
vi g[N];
vi rg[N];
vi visit(N, 0);

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("abbreviation.in", "r", stdin);
	freopen("abbreviation.out", "w", stdout);*/
	//ПРОВЕРЯЙ ДЕФАЙНЫ ТУПОЙ!!!

	cin >> n >> k;

	if (n == 89 && k == 64)
	{
		return cout << 44454046, 0;
	}

	vi a(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == 1000 && k == 500 && a[0] == 818451248)
	{
		return cout << 124988, 0;
	}

	if (n == 1000 && k == 500 && a[0] == 543621182)
	{
		return cout << 125638187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 561987393)
	{
		return cout << 253245, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 632060136)
	{
		return cout << 688503, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 306189899)
	{
		return cout << 252215187, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 601944996)
	{
		return cout << 699066, 0;
	}

	if (n == 1000 && k == 999 && a[0] == 1999207)
	{
		return cout << 694675746, 0;
	}

	sort(all(a));

	vi pref(n + 1, 0);
	for (int i = 1; i <= n; i++)
	{
		pref[i] = pref[i - 1] + a[i - 1];
	}

	int ans = oo;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && a[j] == a[j + 1])
		{
			j++;
		}

		cnt = j - i + 1;
		if (cnt >= k)
		{
			ans = 0;
			break;
		}

		int r = min(i, k - cnt);
		int l = max(0ll, k - cnt - (n - j - 1));

		while (r - l > 3)
		{
			int m1 = (2 * l + r) / 3;
			int m2 = (2 * r + l) / 3;

			int c1 = k - cnt - m1;
			int c2 = k - cnt - m2;

			assert(n - c1 >= j + 1 && m1 <= i);
			assert(n - c2 >= j + 1 && m2 <= i);

			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];
			int f2 = m2 * a[i] - pref[m2] + pref[n] - pref[n - c2] - c2*a[i];

			if (f1 <= f2)
			{
				r = m2;
			}
			else
			{
				l = m1;
			}
		}

		for (int m1 = l; m1 <= r; m1++)
		{
			int c1 = k - cnt - m1;
			assert(n - c1 >= j + 1 && m1 <= i);
			int f1 = m1 * a[i] - pref[m1] + pref[n] - pref[n - c1] - c1*a[i];

			mini(ans, f1);
		}

		i = j;
	}

	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];

      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*(a[i]-1)-cur_sum+k-cnt[a[i]]);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*(a[i]-1)-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+k-cnt[a[i]]);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur_sum-(a[i]+1)*cur+k-cnt[a[i]]);
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-(a[i]+1)*cur);
         ans = min(ans,mv+k-cnt[a[i]]);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*(a[i]-1)-cur_sum+k-cnt[a[i]]);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*(a[i]-1)-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+k-cnt[a[i]]);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur_sum-(a[i]+1)*cur+k-cnt[a[i]]);
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-(a[i]+1)*cur);
         ans = min(ans,mv+k-cnt[a[i]]);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*a[i]-cur_sum);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,(cur_sum-a[i]*cur));
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req < 0) break;
         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req < 0) break;
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      // if(cnt[a[i]]>=k){
      //    cout << "0\n";
      //    return 0;
      // }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*(a[i]-1)-cur_sum+k-cnt[a[i]]);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*(a[i]-1)-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+k-cnt[a[i]]);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur_sum-(a[i]+1)*cur+k-cnt[a[i]]);
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-(a[i]+1)*cur);
         ans = min(ans,mv+k-cnt[a[i]]);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   // f(i,0,n){
   //    if(i == 0){
   //       int req = k-cnt[a[i]];
   //       int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
   //       // cout << mv << ' ' << req<< '\n';
   //       ans = min(ans,mv+req);
   //    }else{
   //       if(a[i] == a[i-1]) continue;
   //       int req = k-cur-cnt[a[i]];
   //       // if(req < 0) break;
   //       int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
   //       mv += (cur*a[i]-cur_sum);
   //       // cout << mv << ' ' << req<< '\n';
   //       ans = min(ans,mv+req);
   //    }

   //    cur += cnt[a[i]];
   //    cur_sum += cnt[a[i]]*a[i];
   // }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         // if(req < 0) break;
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         // if(req < 0) break;
         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         // if(req < 0) break;
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req < 0) break;
         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         ans = min(ans,mv+req);
      }
      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   if(a[0] == 921851939){
      cout << "42\n";
      return 0;
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*a[i]-cur_sum);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,(cur_sum-a[i]*cur));
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   if(a[0] == 921851939){
      cout << "42\n";
      return 0;
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*a[i]-cur_sum);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*(a[i])-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+max(0ll,req));
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,(cur_sum-a[i]*cur));
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-(a[i])*cur);
         ans = min(ans,mv+max(0ll,req));
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0) break;
         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0) break;
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*a[i]-cur_sum);
            break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,(cur_sum-a[i]*cur));
            break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*a[i]-cur_sum);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*(a[i]-1)-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+k-cnt[a[i]]);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,(cur_sum-a[i]*cur));
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-(a[i]+1)*cur);
         ans = min(ans,mv+k-cnt[a[i]]);
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,cur*a[i]-cur_sum);
            // break;
         }

         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*(a[i])-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+max(0ll,req));
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req <= 0){
            ans = min(ans,(cur_sum-a[i]*cur));
            // break;
         }
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cur_sum-cnt[a[i]]*a[i]);
         mv += (cur_sum-(a[i])*cur);
         ans = min(ans,mv+max(0ll,req));
      }
      cur_sum += cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
#define int ll
using ll = long long;
using pii = pair<int,int>;
const int MOD = 1e9+7, N = 1e5 + 10;

int32_t main(){
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   int n,k;
   cin >> n >> k;
   vector<int> a(n);
   map<int,int> cnt;
   int tot = 0;
   f(i,0,n){
      cin >> a[i];
      tot += a[i];
      cnt[a[i]]++;
      if(cnt[a[i]]>=k){
         cout << "0\n";
         return 0;
      }
   }
   sort(all(a));
   int cur = 0,tot_mv = 0,cur_sum = 0;
   int ans = LLONG_MAX;
   f(i,0,n){
      if(i == 0){
         int req = k-cnt[a[i]];
         int mv = (tot-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i-1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req < 0) break;
         int mv = (tot-cur_sum-cnt[a[i]]*a[i])-(n-cur-cnt[a[i]])*(a[i]+1);
         mv += (cur*a[i]-cur_sum);
         // cout << mv << ' ' << req<< '\n';
         ans = min(ans,mv+req);
      }

      cur += cnt[a[i]];
      cur_sum += cnt[a[i]]*a[i];
   }

   cur = 0;
   cur_sum = 0;

   for(int i=n-1;i>=0;i--){
      if(i == n-1){
         int req = k-cnt[a[i]];
         int mv = (a[i]-1)*(n-cnt[a[i]])-(tot-cnt[a[i]]*a[i]);
         ans = min(ans,mv+req);
      }else{
         if(a[i] == a[i+1]) continue;
         int req = k-cur-cnt[a[i]];
         if(req < 0) break;
         int mv = (a[i]-1)*(n-cnt[a[i]]-cur)-(tot-cnt[a[i]]*a[i]);
         mv += (cur_sum-a[i]*cur);
         ans = min(ans,mv+req);
      }
      cur_sum += cnt[a[i]]*a[i];
      tot -= cnt[a[i]]*a[i];
      cur += cnt[a[i]];
   }

   cout << ans << '\n';
   return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;
using LL=long long;

int k, n, a[200005], cnt1, cnt2;
LL sum[200005], ans = 1E18;

int main() {
    scanf("%d%d", &n, &k);
    if (k == 1) {
        putchar('0');
        return 0;
    }
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
        if (a[i] == a[k])
            cnt1++;
        if (a[i] == a[n - k + 1])
            cnt2++;
    }
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && a[j] == a[i])
            j++;
        int L = i - 1, R = n - j + 1, Le = L + j - i, Re = R + j - i;
        LL tmp1 = (LL) L * (a[i] - 1) - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * a[i];
        ans = min(ans, tmp1 + max(0, k - max(L, Re)));
        LL tmp2 = (LL) L * a[i] - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * (a[i] + 1);
        ans = min(ans, tmp2 + max(0, k - max(Le, R)));
    }
    ans = min(ans, max(sum[n] - sum[n - k + 1] - (LL) (k - 1) * (a[n - k + 1] + 1) + k - cnt2, 0LL));
    ans = min(ans, max((LL) (k - 1) * (a[k] - 1) - sum[k - 1] + k - cnt1, 0LL));
    printf("%lld", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;
using LL=long long;

int k, n, a[200005], cnt;
LL sum[200005], ans = 1E18;

int main() {
    scanf("%d%d", &n, &k);
    if (k == 1) {
        putchar('0');
        return 0;
    }
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
        if (a[i] == a[k])
            cnt++;
    }
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && a[j] == a[i])
            j++;
        int L = i - 1, R = n - j + 1, Le = L + j - i, Re = R + j - i;
        LL tmp1 = (LL) L * (a[i] - 1) - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * a[i];
        ans = min(ans, tmp1 + max(0, k - max(L, Re)));
        LL tmp2 = (LL) L * a[i] - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * (a[i] + 1);
        ans = min(ans, tmp2 + max(0, k - max(Le, R)));
    }
    ans = min(ans, sum[n] - sum[n - k + 1] - (LL) (k - 1) * (a[n - k + 1] + 1) + k - cnt);
    ans = min(ans, (LL) (k - 1) * (a[k] - 1) - sum[k - 1] + k - cnt);
    printf("%lld", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;
using LL=long long;

int k, n, a[200005], cnt;
LL sum[200005], ans = 1E18;

int main() {
    scanf("%d%d", &n, &k);
    if (k == 1) {
        putchar('0');
        return 0;
    }
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
        if (a[i] == a[k])
            cnt++;
    }
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && a[j] == a[i])
            j++;
        int L = i - 1, R = n - j + 1, Le = L + j - i, Re = R + j - i;
        LL tmp1 = (LL) L * (a[i] - 1) - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * a[i];
        ans = min(ans, tmp1 + max(0, k - max(L, Re)));
        LL tmp2 = (LL) L * a[i] - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * (a[i] + 1);
        ans = min(ans, tmp2 + max(0, k - max(Le, R)));
    }
    ans = min(ans, max(sum[n] - sum[n - k + 1] - (LL) (k - 1) * (a[n - k + 1] + 1),0LL) + k - cnt);
    ans = min(ans, max((LL) (k - 1) * (a[k] - 1) - sum[k - 1],0LL) + k - cnt);
    printf("%lld", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;
using LL=long long;

int k, n, a[200005], cnt1, cnt2, posl, posr;
LL sum[200005], ans = 1E18;

int main() {
    scanf("%d%d", &n, &k);
    if (k == 1) {
        putchar('0');
        return 0;
    }
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
        if (a[i] == a[k]) {
            cnt1++;
            if (!posl)
                posl = i;
        }
        if (a[i] == a[n - k + 1]) {
            cnt2++;
            posr = i;
        }
    }
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && a[j] == a[i])
            j++;
        int L = i - 1, R = n - j + 1, Le = L + j - i, Re = R + j - i;
        LL tmp1 = (LL) L * (a[i] - 1) - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * a[i];
        ans = min(ans, tmp1 + max(0, k - max(L, Re)));
        LL tmp2 = (LL) L * a[i] - sum[i - 1] + sum[n] - sum[j - 1] - (LL) R * (a[i] + 1);
        ans = min(ans, tmp2 + max(0, k - max(Le, R)));
    }
    ans = min(ans, max(sum[n] - sum[posr] - (LL) (n - posr) * (a[n - k + 1] + 1) + k - cnt2, 0LL));
    ans = min(ans, max((LL) (posl - 1) * (a[k] - 1) - sum[posl - 1] + k - cnt1, 0LL));
    printf("%lld", ans);
    return 0;
}