You are given the array $a$ consisting of $n$ elements and the integer $k \le n$.

You want to obtain at least $k$ equal elements in the array $a$. In one move, you can make one of the following two operations:

  Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of $a$ is $mn$ then you choose such index $i$ that $a_i = mn$ and set $a_i := a_i + 1$);  take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of $a$ is $mx$ then you choose such index $i$ that $a_i = mx$ and set $a_i := a_i - 1$). 

Your task is to calculate the minimum number of moves required to obtain at least $k$ equal elements in the array.


-----Input-----

The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of elements in $a$ and the required number of equal elements.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$.


-----Output-----

Print one integer — the minimum number of moves required to obtain at least $k$ equal elements in the array.


-----Examples-----
Input
6 5
1 2 2 4 2 3

Output
3

Input
7 5
3 3 2 1 1 1 3

Output
4

Difficulty: introductory

Starter code:

Solutions:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    a = LI()
    a.sort()
    d = defaultdict(lambda : 0)
    c = defaultdict(lambda : 0)
    s = [0]
    for i in a:
        d[i] += i
        c[i] += 1
        s.append(s[-1]+i)
    ans = float("inf")
    p = -1
    for i in a:
        if i == p:
            continue
        if k <= c[i]:
            ans = 0
            break
        l,r = bisect.bisect_left(a,i),bisect.bisect_right(a,i)
        m = r
        if m >= k:
            ns = l*(i-1)-s[l]
            su = ns+k-c[i]
            if su < ans:
                ans = su
        m = n-l
        if m >= k:
            ns = s[n]-s[r]-(n-r)*(i+1)
            su = ns+k-c[i]
            if su < ans:
                ans = su
        ns = s[n]-s[r]-(n-r)*(i+1)+l*(i-1)-s[l]
        su = ns+k-c[i]
        if su < ans:
            ans = su
        p = i
    print(ans)
    return

#Solve
def __starting_point():
    solve()

__starting_point()

from collections import Counter

import sys
from itertools import accumulate


#
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__

def go():
    n, k = list(map(int, input().split()))
    a = sorted(map(int, input().split()))
    c = Counter(a)
    if max(c.values()) >= k:
        return 0

    upsum = {}
    upcnt = {}
    prev = -1
    cursum = 0
    for cnt, aa in enumerate(a):
        if aa != prev:
            upsum[aa] = cursum
            upcnt[aa] = cnt
        cursum += aa
        prev = aa

    downsum = {}
    downcnt = {}
    prev = -1
    cursum = 0
    for cnt, aa in enumerate(reversed(a)):
        if aa != prev:
            downsum[aa] = cursum
            downcnt[aa] = cnt
        cursum += aa
        prev = aa

    best = cursum
    for target, cnt in list(c.items()):
        need = k - cnt
        if upcnt[target] >= need:
            best = min(best, (target - 1) * upcnt[target] - upsum[target] + need)
        if downcnt[target] >= need:
            best = min(best, downsum[target] - (target + 1) * downcnt[target] + need)
        best = min(best, (target - 1) * upcnt[target] - upsum[target]
                   + downsum[target] - (target + 1) * downcnt[target] +need
                   )

    return best


# x,s = map(int,input().split())
# t = int(input())
t = 1
# ans = []
for _ in range(t):
    print(go())
    # ans.append(str(go()))
#
# print('\n'.join(ans))


def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    A.append(0)
    l1=[0]*n
    for i in range(1,n):
        l1[i]=l1[i-1]+(A[i]-A[i-1])*i 
    l2=[0]*n
    for i in range(1,n):
        l2[n-i-1]=l2[n-i]+(A[n-i]-A[n-i-1])*i 
    ans=float("INF")
    c=1
    for i in range(n):
        if A[i+1]==A[i]:
            c+=1
        else:
            c1=i-c+1
            c2=n-i-1
            if c>=k:
                ans=0
            elif c+c1>=k or c+c2>=k:
                if c+c1>=k:
                	ans=min(ans,l1[i]-c-c1+k)
                if c+c2>=k:
                	ans=min(ans,l2[i]-c-c2+k)
            else:
                ans=min(ans,l1[i]+l2[i]-n+k)
            c=1
    print(ans)
def __starting_point():
    main()
__starting_point()

from collections import Counter
from bisect import bisect, bisect_left
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = Counter(a)
c = sorted(Counter(a).items())
keys = sorted(Counter(a).keys())

m = len(c)
l_cum = [c[0][0] * c[0][1]]
l_cnt = [c[0][1]]
for i in range(1, m):
	l_cum.append(l_cum[-1] + c[i][0] * c[i][1])
	l_cnt.append(l_cnt[-1] + c[i][1])


r_cum = [0 for _ in range(m)]
r_cum[-1] = c[-1][0] * c[-1][1]
r_cnt = [0 for _ in range(m)]
r_cnt[-1] = c[-1][1]
for i in range(m-2, -1, -1):
	r_cum[i] = r_cum[i+1] + c[i][0] * c[i][1]
	r_cnt[i] = r_cnt[i+1] + c[i][1]

#print(l_cum)
#print(r_cum)

ans = 10**20
for i in range(m):
	c_num = a[keys[i]]
	if c_num >= k:
		ans = 0
		break

	if i > 0:
		l_res = (keys[i]-1) * l_cnt[i-1] - l_cum[i-1]
		l_num = l_cnt[i-1]
	else:
		l_res = 10**30
		l_num = 0
	if i < m-1:
		r_res = r_cum[i+1] - (keys[i]+1) * r_cnt[i+1]
		r_num = r_cnt[i+1]
	else:
		r_res = 10**30
		r_num = 0

	if c_num + l_num < k and c_num + r_num < k:
		ans = min(ans, l_res + r_res + k - c_num)
	elif c_num + l_num < k:
		ans = min(ans, r_res + k - c_num)
	elif c_num + r_num < k:
		ans = min(ans, l_res + k - c_num)
	else:
		ans = min(ans, min(r_res, l_res) + k - c_num)
	#print(ans)

print(ans)

n, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
sums, suf, q1 = [0]*n, [n]*n, n
for q in range(n-2, -1, -1):
    sums[q] = sums[q + 1]
    if a[q] != a[q1-1]:
        sums[q] += (n-q1)*(a[q1-1]-a[q])+(q1-q-1)*(a[q1-1]-a[q]-1)
        q1 = q + 1
    suf[q] = q1
ans, sum1, q1 = sums[0]+k-suf[0] if suf[0] < k else 0, 0, -1
for q in range(1, n):
    if a[q] != a[q1 + 1]:
        sum1 += (q1+1)*(a[q]-a[q1+1])+(q-q1-1)*(a[q]-a[q1+1]-1)
        q1 = q - 1
    if suf[q]-q1-1 >= k:
        ans = 0
        break
    if k <= suf[q]:
        ans = min(ans, sum1+k-suf[q]+q1+1)
    if k < n-q1:
        ans = min(ans, sums[q]+k-suf[q]+q1+1)
    ans = min(ans, sum1+sums[q]+k-suf[q]+q1+1)
print(ans)