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.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.KeyStore.Entry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Main {
	
	public static void main(String[] args) throws Exception {
		Scanner sc=new Scanner(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=sc.nextInt();
		int k=sc.nextInt();
		int a[]=new int[n];
		int b[]=new int[n];
		for (int i = 0; i < a.length; i++) {
			a[i]=sc.nextInt();
		b[i]=a[i];
		}
		a=schuffle(a);
		
		Arrays.sort(a);
		int count1[]=new int[(int) (2*1e6+1)];
		for (int i = 0; i < a.length; i++) {
			count1[a[i]]++;
		}
		for (int i = 0; i < a.length; i++) {
			if(count1[a[i]]>=k) {
				System.out.println(0);
				return;
			}
			
		}
		TreeMap<Integer,Integer> map=new TreeMap<>();
		int count[]=new int[(int) (2*1e6+1)];
		for (int i = 0; i < a.length; i++) {
		int x=a[i];
		map.put(x,map.getOrDefault(x,0)+1);
		while(x!=0) {
			
			x=x/2;
			if(map.get(x)!=null&&map.get(x)==k) {
				continue;
			}
			map.put(x,map.getOrDefault(x,0)+1);
			int y=0;
			int x2=0;
			double x1=0;
			if(x!=0&&x!=1) { 
			double z=Math.log(a[i])/Math.log(2);
			z=Math.floor(z+0.000000001);
			 x1=Math.log(x)/Math.log(2);
			x1=Math.floor(x1+0.00000001);
			 x2=(int) x1;
			 y=(int) z;
			 count[x]+=y-x2;
				
			}
			
			if(x==0&&a[i]>1) {
				double z=Math.log(a[i])/Math.log(2);
				z=Math.ceil(z+0.000000001);
				 y=(int) z;	
				count[x]+=y;
			}
			else if(x==0&&a[i]==1) {
				count[x]++;
			}
			else if(x==1&&a[i]>1) {
				double z=Math.log(a[i])/Math.log(2);
				z=Math.floor(z+0.000000001);
				 y=(int) z;	
			
				 count[x]+=y;
				
			}
			
		}
		}
		ArrayList<Integer> l=new ArrayList<>();
		for(java.util.Map.Entry<Integer, Integer> entry : map.entrySet()) {
		int x=entry.getKey();
		int y=entry.getValue();
		if(y>=k) {
			l.add(x);
			//System.out.println(x+" "+y);
		}
		}
		int min=(int) 1e9;
		for(int x:l) {
		min=Math.min(count[x], min);
		}
		System.out.println(min);
		
		
		out.flush();
	}	
	public static int[] schuffle(int a[]) {
		for (int i = 0; i < a.length; i++) {
			int x=(int)(Math.random()*a.length);
		int temp=a[x];
		a[x]=a[i];
		a[i]=temp;
		}
		return a;
	}
		static int V;
	static int INF=(int) 1E9;
		static class Edge implements Comparable<Edge>
		{
			int node, cost;
			
			Edge(int a, int b) { node = a;	cost = b; }
			
			public int compareTo(Edge e){ return cost - e.cost;	}
		}
		static class Edge2 implements Comparable<Edge2>
		{
			int node, cost;
			int p;
			Edge2(int a, int b,int c) { node = a;	cost = b; p=c;}
			
			public int compareTo(Edge2 e){ return cost - e.cost;	}
		}

		static int[] []dijkstra(int S, int T)	//O(E log E)
		{
			
			int[] []dist = new int[3][V];
			Arrays.fill(dist[0], INF);
			
			Arrays.fill(dist[1], INF);
			Arrays.fill(dist[2], INF);
			
			PriorityQueue<Edge2> pq = new PriorityQueue<Edge2>();
			
			dist[0][S] = 0;
			pq.add(new Edge2(S, 0,0));						//may add more in case of MSSP (Mult-Source)
			while(!pq.isEmpty())
			{
				Edge2 cur = pq.remove();
				int p=cur.p;
				if(cur.node==T) {
					continue;
				}
				for(Edge nxt: adjList[cur.node])
				{	int roads=(p+1)%3;
					
					if(cur.cost + nxt.cost < dist[roads][nxt.node]) {
						pq.add(new Edge2(nxt.node, dist[roads][nxt.node] = cur.cost + nxt.cost,roads));
					}
					}
			}
			return dist;
		}		
	static ArrayList<Edge>adjList[];
    static class Scanner {
		StringTokenizer st;
		BufferedReader br;
 
		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}
 
		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
 
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public char nextChar() throws IOException {
			return next().charAt(0);
		}
 
		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}	
}