As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.

Elections are coming. You know the number of voters and the number of parties — n and m respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give i-th voter c_i bytecoins you can ask him to vote for any other party you choose.

The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.

Input

The first line of input contains two integers n and m (1 ≤ n, m ≤ 3000) — the number of voters and the number of parties respectively.

Each of the following n lines contains two integers p_i and c_i (1 ≤ p_i ≤ m, 1 ≤ c_i ≤ 10^9) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision.

The United Party of Berland has the index 1.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples

Input

1 2
1 100


Output

0


Input

5 5
2 100
3 200
4 300
5 400
5 900


Output

500


Input

5 5
2 100
3 200
4 300
5 800
5 900


Output

600

Note

In the first sample, The United Party wins the elections even without buying extra votes.

In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 3, 4 and 5 get one vote and party number 2 gets no votes.

In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
//package july23to31;

import java.io.*;
import java.util.*;

public class Round501 {
    InputStream is;
    PrintWriter out;
    String INPUT = "";
    //boolean codechef=true;
    boolean codechef=true;
    
	void solve()
	{
		int n = ni(), m = ni();
		int[] from = new int[n];
		int[] to = new int[n];
		for(int i = 0;i < n;i++){
			from[i] = ni()-1;
			to[i] = ni();
		}
		int[][] g = packD(m, from, to);
		
		for(int i = 0;i < m;i++){
			Arrays.sort(g[i]);
		}
		
		int[] table = new int[n];
		long min = Long.MAX_VALUE;
		for(int d = Math.max(1, g[0].length);d <= n;d++){
			int need = d - g[0].length;
			int p = 0;
			long b = 0;
			for(int i = 1;i < m;i++){
				for(int j = 0;j < g[i].length-d+1;j++){
					need--;
					b += g[i][j];
				}
				for(int j = Math.max(0, g[i].length-d+1);j < g[i].length;j++){
					table[p++] = g[i][j];
				}
			}
			//System.out.println(need);
			Arrays.sort(table, 0, p);
			//System.out.println(Arrays.toString(table));
			int q = 0;
			while(need > 0){
				b += table[q++];
				need--;
			}
			min = Math.min(min, b);
		}
		out.println(min);
	}
	
	static int[][] packD(int n, int[] from, int[] to) {
		int[][] g = new int[n][];
		int[] p = new int[n];
		for (int f : from)
			p[f]++;
		for (int i = 0; i < n; i++)
			g[i] = new int[p[i]];
		for (int i = 0; i < from.length; i++) {
			g[from[i]][--p[from[i]]] = to[i];
		}
		return g;
	}
    
    public boolean check(int[] cnt,int m)
    {
    	boolean ch=true;
    	for(int i=2;i<=m;i++)
		{
			if(cnt[i]>=cnt[1])
			{
				ch=false;break;
			}
		}
		return ch;
    }
    
    static long[] dp;
    
    public long comp(Pair[] arr,int st,int end,int [] cnt,int m)
    {
    	boolean ch=check(cnt,m);
    	//System.out.println("st:"+st);
    	//System.out.println(Arrays.toString(cnt));
    	if(ch)
    	{
    		//System.out.println(st+" "+Arrays.toString(cnt));
    		return 0;
    	}
    	if(st==end)
    	{
    		cnt[arr[st].a]--;
    		cnt[1]++;
    		ch=check(cnt,m);
    		if(ch)
    		{
    			cnt[arr[st].a]++;
        		cnt[1]--;
    			return arr[st].b;
    		}
    		else
    		{
    			cnt[arr[st].a]++;
        		cnt[1]--;
        		return Long.MAX_VALUE;
    		}
    	}
    	//if(dp[st]!=-1)return dp[st];
    	
    	long min=comp(arr,st+1,end,cnt,m);
    	cnt[arr[st].a]--;
    	cnt[1]++;
    	long val=comp(arr,st+1,end,cnt,m);
    	if(val==Long.MAX_VALUE)val=Long.MAX_VALUE-arr[st].b;
    	min=Math.min(min,arr[st].b+val);
    	cnt[arr[st].a]++;
    	cnt[1]--;
    	dp[st]=min;
    	//System.out.println(min);
    	return min;
    }
    
    static class Temp
    {
    	int diff;
    	int id1;int id2;
    	int a2b;
    	public Temp(int diff,int id1,int id2,int a2b)
    	{
    		this.diff=diff;
    		this.id1=id1;
    		this.id2=id2;
    		this.a2b=a2b;
    	}
    }
    
    void rev(int[] arr,int l,int r)
    {
    	int sz=(r-l+1);
    	for(int i=l;i<l+(sz/2);i++)
    	{
    		swap(arr,i,r--);
    	}
    }
    
    void swap(int[] arr,int i1,int i2)
    {
    	int tmp=arr[i1];
    	arr[i1]=arr[i2];
    	arr[i2]=tmp;
    }
    static int ans;
    
    static int[] finArr;
    
    void permute(int[] arr, int l, int r)
    {
       int i;
       if (l == r)
       {
    	   //System.out.println(Arrays.toString(arr));
    	   int lis=lis(arr,arr.length),lds=lds(arr,arr.length);
    	   //System.out.println(lis+" "+lds);
    	   if(ans>(lis==0?1:lis)+(lds==0?1:lds))
    	   {
    		   ans=(lis==0?1:lis)+(lds==0?1:lds);
    		   finArr=Arrays.copyOf(arr, arr.length);
    	   }
       }
       else
       {
           for (i = l; i <= r; i++)
           {
              swap(arr,l,i);
              permute(arr, l+1, r);
              swap(arr,l,i);
           }
       }
    }
    
    static int lds(int arr[], int n)
    {
        int lds[] = new int[n];
        int i, j, max = 0;
     
        // Initialize LDS with 1 
        // for all index. The minimum 
        // LDS starting with any
        // element is always 1
        for (i = 0; i < n; i++)
            lds[i] = 1;
     
        // Compute LDS from every 
        // index in bottom up manner
        for (i = 1; i < n; i++)
            for (j = 0; j < i; j++)
                if (arr[i] < arr[j] && 
                             lds[i] < lds[j] + 1)
                    lds[i] = lds[j] + 1;
     
        // Select the maximum 
        // of all the LDS values
        for (i = 0; i < n; i++)
            if (max < lds[i])
                max = lds[i];
     
        // returns the length
        // of the LDS
        return max;
    }
    
    static int lis(int arr[],int n)
    {
          int lis[] = new int[n];
          int i,j,max = 0;
 
          /* Initialize LIS values for all indexes */
           for ( i = 0; i < n; i++ )
              lis[i] = 1;
 
           /* Compute optimized LIS values in bottom up manner */
           for ( i = 1; i < n; i++ )
              for ( j = 0; j < i; j++ ) 
                         if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
                    lis[i] = lis[j] + 1;
 
           /* Pick maximum of all LIS values */
           for ( i = 0; i < n; i++ )
              if ( max < lis[i] )
                 max = lis[i];
 
            return max;
    }

	public static long[] radixSort(long[] f){ return radixSort(f, f.length); }
	public static long[] radixSort(long[] f, int n)
	{
		long[] to = new long[n];
		{
			int[] b = new int[65537];
			for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++;
			for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
			for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i];
			long[] d = f; f = to;to = d;
		}
		{
			int[] b = new int[65537];
			for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++;
			for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
			for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i];
			long[] d = f; f = to;to = d;
		}
		{
			int[] b = new int[65537];
			for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++;
			for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
			for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i];
			long[] d = f; f = to;to = d;
		}
		{
			int[] b = new int[65537];
			for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++;
			for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
			for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i];
			long[] d = f; f = to;to = d;
		}
		return f;
	}
	
	static class DSets
	{
		int[] p;
		int[] r;
		int[] s;
		int size;
		int max_component;
		public DSets(int n)
		{
			s=new int[n+1];
			r=new int[n+1];
			p=new int[n+1];
			for(int i=1;i<=n;i++)
			{
				r[i]=0;
				p[i]=i;
				s[i]=1;
			}
			this.size=n+1;
		}
		public int find(int x)
		{
			if(x != p[x]){
	            p[x] = find(p[x]) ;
	        }
	        return p[x] ;
		}
		public boolean unionBySize(int x,int y)
		{
			int rx , ry ;
	        rx = find(x) ;
	        ry = find(y) ;
	        if(rx != ry)
	        {
	            if(r[rx] < r[ry])
	            {
	                p[rx] = ry ;
	                s[ry] += s[rx] ;
	                max_component = Math.max(max_component,s[ry]) ;
	            }
	            else
	            {
	                p[ry] = rx ;
	                s[rx] += s[ry] ;
	                max_component = Math.max(max_component,s[rx]) ;
	            }
	            if(r[rx] == r[ry])
	            {
	                r[rx] ++;
	            }
	            return true ;
	        }
	        return false ;
		}
	}
    
    static class Pair
    {
        int a,b;
        public Pair(int a,int b)
        {
            this.a=a;
            this.b=b;
        }
    }
    
    static int bit[];
    
    static void add(int x,int d,int n)
    {
        for(int i=x;i<=n;i+=i&-i)bit[i]+=d;
    }

    static int query(int x)
    {
        int ret=0;
        for(int i=x;i>0;i-=i&-i)
        ret+=bit[i];
        return ret;
    }
    
    static long lcm(int a,int b)
    {
        long val=a;
        val*=b;
        return (val/gcd(a,b));
    }
    
    static int gcd(int a,int b)
    {
        if(a==0)return b;
        return gcd(b%a,a);
    }
    
    static int pow(int a, int b, int p)
    {
        long ans = 1, base = a;
        while (b!=0)
        {
            if ((b & 1)!=0)
            {
                ans *= base;
                ans%= p;
            }
            base *= base;
            base%= p;
            b >>= 1;
        }
        return (int)ans;
    }

    static int inv(int x, int p)
    {
        return pow(x, p - 2, p);
    }

    
    void run() throws Exception
    {
        if(codechef)oj=true;
        is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);
        
        long s = System.currentTimeMillis();
        solve();
        out.flush();
        tr(System.currentTimeMillis()-s+"ms");
    }
    
    public static void main(String[] args) throws Exception {new Round501().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 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();
        }
    }
    
    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
