Problem: You are given the array a consisting of n elements and the integer k ≤ 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 ≤ k ≤ n ≤ 2 ⋅ 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, ..., a_n (1 ≤ a_i ≤ 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

Solution (in JAVA):

Solution:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
 
 
import java.util.*;
 
public class KEqual1328F {
 
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	private void sort(int[] arr, int start, int end){
        if(start >= end) return;
        int mid = start + (end - start) / 2;
        sort(arr, start, mid);
        sort(arr, mid +1, end);
        merge(arr, start, mid, end);
    }
	private void merge(int[] arr, int start, int mid, int end){
        int i = start, j = mid + 1;
        int[] help = new int[end - start + 1];
        int c = 0;
        while(i <= mid && j <= end){
            help[c++] = arr[j] < arr[i] ? arr[j++] : arr[i++];
        }
        while(i <= mid){
            help[c++] = arr[i++];
        }
        while(j <= end){
            help[c++] = arr[j++];
        }
        for(int k = 0; k < help.length; k++){
            arr[start + k] = help[k];
        }
    }
	void solve() {
		int n = ni();
		int k = ni();
		int a[] = new int[n];
		HashMap<Integer , Integer> hm = new HashMap<>();
//		
//		int xy = Integer.MAX_VALUE;
//		int yz = Integer.MIN_VALUE;
		
		boolean flag = false;
		for(int i = 0 ; i< n ; i++) {
			//a[i] = new Random().nextInt((561987918 - 561986918) + 1) + 561986918;
			a[i] = ni();
			hm.put(a[i] , hm.getOrDefault(a[i] , 0)+1);
			
			if(hm.get(a[i]) == k) {
				flag = true;
			}
//			xy = Math.min(xy, a[i]);
//			yz = Math.max(yz, a[i]);
//			hs.add(a[i]);
		}
		
		if(flag) {
			out.println(0);
			return;
		}
		
//		if(a[0] == 561987393) {
//			out.println(hs.size());
//			out.println(xy);
//			out.println(yz);
//		}
		long minimum = Long.MAX_VALUE;
		sort(a, 0, n-1);
		
//		for(int i : a) out.print(i+" ");
//		out.println();
		
		long min[][] = new long[n][2];
		min[0][0] = 0;
		min[0][1] = 1;
		int prev = a[0];
		
		for(int i = 1 ; i<n ; i++) {
			if(a[i] == a[i-1]) {
				if(min[i-1][1] < k) {
					min[i][0] = min[i-1][0];
					min[i][1] = min[i-1][1]+1;
				}
				else {
					min[i][0] = min[i-1][0]-(a[i]-prev);
					min[i][1] = k;
				}
			}
			else {
				long m = Math.min(min[i-1][1], k-1);
				min[i][0] = min[i-1][0]+(a[i]-a[i-1])*1l*m;
				min[i][1] = m+1;
				prev = a[i-1];
			}
		}
		
		long max[][] = new long[n][2];
		max[n-1][0] = 0;
		max[n-1][1] = 1;
		prev = a[n-1];
		
		for(int i = n-2 ; i>=0 ; i--) {
			if(a[i] == a[i+1]) {
				if(max[i+1][1] < k) {
					max[i][0] = max[i+1][0];
					max[i][1] = max[i+1][1]+1;
				}
				else {
					max[i][0] = max[i+1][0]-(prev-a[i]);
					max[i][1] = k;
				}
			}
			else {
				long m = Math.min(max[i+1][1], k-1);
				max[i][0] = max[i+1][0]+(a[i+1]-a[i])*1l*m;
				max[i][1] = m+1;
				prev = a[i+1];
			}
			
			
		}
		
//		for(long i[] : min) {
//			out.println(i[0]+" "+i[1]);
//		}
//		out.println();
//		for(long i[] : max) {
//			out.println(i[0]+" "+i[1]);
//		}
		
		
		for(int i = 0 ; i<n ; i++) {
			if(min[i][0] < max[i][0]) {
				long val = min[i][0];
				long req = k-min[i][1];
				if(req > 0 && i+1 < n) {
					val += max[i+1][0] + req+max[i+1][1]*(a[i+1] - a[i] - 1);
				} 
				//out.println(val);
				
				minimum = Math.min(minimum, val);
			}
			else {
				long val = max[i][0];
				long req = k-max[i][1];
				if(req > 0 && i-1 >= 0) {
					val += min[i-1][0] + req+min[i-1][1]*(a[i] - a[i-1] - 1);
				} 
				
				minimum = Math.min(minimum, val);
			}
			if(min[i][1] == k) minimum = Math.min(minimum, min[i][0]);
			if(max[i][1] == k) minimum = Math.min(minimum, max[i][0]);
		} 
		minimum = minimum < 0 ? 0 : minimum;
		
		out.println(minimum);
		
	}
	
	/*
	 * 
	 * 10 8
561987648 561987058 561987340 561987350 561987472 561987671 561987470 561987698 561987204 561987083 
1684
	 */
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
 
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
		//tr(System.currentTimeMillis()-s+"ms");
	}
 
	public static void main(String[] args) throws Exception { new KEqual1328F().run(); }
 
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
 
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
 
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
 
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
 
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
 
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
 
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
 
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
 
	private int[][] na(int n , int m)
	{
		int[][] a = new int[n][m];
		for(int i = 0;i < n;i++) 
			for(int j = 0 ; j<m ; j++) a[i][j] = ni();
		return a;
	}
 
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
 
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
 
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
 
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
 
	void display2D(int a[][]) {
		for(int i[] : a) {
			for(int j : i) {
				out.print(j+" ");
			}
			out.println();
		}
	}
 
	int[][] creategraph(int n , int m) {
		int g[][] = new int[n+1][];
 
		int from[] = new int[m];
		int to[] = new int[m];
		int ct[] = new int[n+1];
 
		for(int i = 0 ; i<m; i++) {
			from[i] = ni();
			to[i] = ni();
			ct[from[i]]++;
			ct[to[i]]++;
		}
 
		int parent[] = new int[n+1];
 
		for(int i = 0 ; i<n+1 ; i++) g[i] = new int[ct[i]];
 
		for(int i = 0 ; i<m ; i++) {
			g[from[i]][--ct[from[i]]] = to[i];
			g[to[i]][--ct[to[i]]] = from[i];
		}
 
		return g;
	}
 
	static long __gcd(long a, long b)
	{
 
		if(b == 0) 
		{
			return a;
		}
		else
		{
			return __gcd(b, a % b);
		}
	}
 
	// To compute x^y under modulo m
	static long power(long x, long y, long p)
	{
 
		// Initialize result
		long res = 1;
 
		// Update x if it is more than or
		// equal to p
		x = x % p;
 
		while (y > 0)
		{
 
			// If y is odd, multiply x
			// with result
			if (y % 2 == 1)
				res = (res * x) % p;
 
			// y must be even now
			y = y >> 1; // y = y/2
		x = (x * x) % p;
		}
 
		return res;
	}
 
	// Function to find modular 
	// inverse of a under modulo m
	// Assumption: m is prime
	static long modInverse(long a, int m)
	{
		if (__gcd(a, m) != 1) {
			//System.out.print("Inverse doesn't exist");
			return -1;
		}
 
		else {
 
			// If a and m are relatively prime, then
			// modulo inverse is a^(m-2) mode m
			//			System.out.println("Modular multiplicative inverse is "
			//											+power(a, m - 2, m));
			return power(a, m - 2, m);
		}
	}
 
	static long nCrModPFermat(int n, int r,
			int p , long fac[])
	{
 
		// Base case
		if (r == 0)
			return 1;
 
		// Fill factorial array so that we
		// can find all factorial of r, n
		// and n-r
		long t = (fac[n]* modInverse(fac[r], p))%p;
 
		return ( (t* modInverse(fac[n-r], p))% p);
	}
 
 
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}