//#define DEBUG

#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
#include <fstream>
#include <iterator>
#include <functional>
#include <queue>
#include <random>
#include <array>

#define per(i,a,b) for (int i = a; i<b; i++)
#define fore(set, it) for (auto it = set.begin(); it!=set.end(); it++)
#define LONG long long
#define pii pair<LONG,LONG>
#define mp make_pair
#define pq priority_queue
#define INF 1000000000000000
#define BIG(x) (x>='A' &&  x<='Z')
#define SMALL(x) (x>='a' && x<='z')
#define toBIG(x) (SMALL(x)?(x-'a'+'A'):x)
#define toSMALL(x) (BIG(x)?(x-'A'+'a'):x)
#define END(s) {cout << s; return 0;}
#define tmin(a,b,c) min(a,min(b,c))
#define tmax(a,b,c) max(a,max(b,c))
#define E2 100
#define E3 1000
#define E4 10000
#define E5 100000
#define E6 1000000
#define E7 10000000LL
#define E8 100000000LL
#define E9 1000000000LL
#define E10 10000000000LL
#define E11 100000000000LL
#define E12 1000000000000LL

using namespace std;

void solve();

int main(int argc, char ** argv)
{
#ifdef DEBUG
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
	cin.tie(0);
	cin.sync_with_stdio(false);
	cout.precision(12);
	cout.setf(ios::fixed);
	solve();
	return 0;
}
#define int LONG

void solve() {
	int q;
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		int k = 0;
		while (n % 2 == 0 && n != 0) {
			n /= 2;
			k++;
		}
		while (n % 3 == 0 && n != 0) {
			n /= 3;
			k += 2;
		}
		while (n % 5 == 0 && n != 0) {
			n /= 5;
			k += 3;
		}
		if (n == 1) {
			cout << k << endl;
		}
		else {
			cout << -1 << endl;
		}
	}
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <list>
#include <utility>
using namespace std;
const int maxn = 200010;
const int inf = 0x3ffffff;
typedef _int64 ll;
int min(int a,int b){return a>b?b:a;}
int max(int a,int b){return a>b?a:b;}
int d[maxn];
const int mod = 998244353;
int main()
{
#ifdef _DEBUG
	freopen("in.txt","r",stdin);
#endif
	int q,n;
	while(q--)
	{
		scanf("%I64d",&n);
		int ans=0;
		while(n%2==0||n%3==0||n%5==0)
		{
		if(n%2==0)
		{
			while(n%2==0)
			{
				n/=2;
				ans++;
			}
		}
		if(n%3==0)
		{
			while(n%3==0)
			{
				n=n*2/3;
				ans++;
			}
		}
		if(n%5==0)
		{
			while(n%5==0)
			{
				n=n*4/5;
				ans++;
			}
		}
		}
		if(n==1)cout<<ans<<endl;
		else cout<<-1<<endl;
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <list>
#include <utility>
using namespace std;
const int maxn = 200010;
const int inf = 0x3ffffff;
typedef _int64 ll;
int min(int a,int b){return a>b?b:a;}
int max(int a,int b){return a>b?a:b;}
int d[maxn];
const int mod = 998244353;
int main()
{
#ifdef _DEBUG
	freopen("in.txt","r",stdin);
#endif
	int q;
	cin>>q;
	ll n;
	while(q--)
	{
		scanf("%I64d",&n);
		int ans=0;
		while(n%2==0||n%3==0||n%5==0)
		{
		if(n%2==0)
		{
			while(n%2==0)
			{
				n/=2;
				ans++;
			}
		}
		if(n%3==0)
		{
			while(n%3==0)
			{
				n=n*2/3;
				ans++;
			}
		}
		if(n%5==0)
		{
			while(n%5==0)
			{
				n=n*4/5;
				ans++;
			}
		}
		}
		if(n==1)cout<<ans<<endl;
		else cout<<-1<<endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
long long a, b;

int main(int argc, char* argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> a;
	while (a--)
	{
		cin >> b;
		int cnt = 0;
		bool ans = true;
		while(1)
		{
			if (b == 1)
				break;
			if (b % 5 == 0)
			{
				b = b * 4 / 5;
				cnt++;
			}
			else if (b % 3 == 0) {
				b = b * 2 / 3;
				cnt++;
			}
			else if (b % 2 == 0)
			{
				b = b / 2;
				cnt++;
			}
			else {
				ans = false;
				break;
			}
		}
		if (ans)
			cout << cnt << endl;
		else
			cout << "-1" << endl;
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef __int64 ll;
using namespace std;

int main()
{
    int q;
    ll n;
    scanf("%d",&q);
    while (q--){
        scanf("%I64d",&n);
        ll ans=0;
        while (n%2==0){
            ans++;
            n/=2;
        }
        while (n%3==0){
            ans+=2;
            n/=3;
        }
        while (n%5==0){
            ans+=3;
            n/=5;
        }
        if (n!=1)
            printf("-1\n");
        else
            printf("%I64d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<deque>
#include<string>
using namespace std;
int main() {
	long long a, x, y = 0;
	deque<long long>d;
	cin >> a;
	for (int i = 0; i < a; i++) {
		cin >> x;
		if (x == 1) {
			d.push_back(0);
			continue;
		}
		while (x != 1) {
			if (x % 5 == 0) {
				x = (4 * x) / 5;
				y++;
			}
			else if (x % 3 == 0) {
				x = (x * 2) / 3;
				y++;
			}
			else if (x % 2 == 0) {
				x /= 2;
				y++;
			}
			else {
				y = -1;
				break;
			}
		}
		d.push_back(y);
		y = 0;
	}
	for (int i = 0; i < a; i++)
		cout << d[i] << endl;
	return 0;
}


#include <iostream>

using namespace std;
long long f(long long x)
{
    int ret = 0;
        while(x%5==0)
        {
            x=x/5*4;
            ret++;
        }
        while(x%3==0)
        {
            x=x/3*2;
            ret++;
        }
        while(x%2==0)
        {
            x=x/2;
            ret++;
        }
        if(x!=1)
            ret=-1;
return ret;

}
int main()
{
    int T;
    long long x,ans;
    cin>>T;
    while(T--)
    {
        cin>>x;
        ans=f(x);
        cout<<ans<<' ';
    }
    return 0;
}


#include<stdio.h>
int main()
{
	int q;
	scanf("%d",&q);
	long long a[1010];
	for(int i=0;i<q;i++)
	{
		scanf("%I64d",&a[i]);
	}
	for(int i=0;i<q;i++)
	{
		long long res=a[i];
		long long k=0;
		int flag=0;
		if(res==1)
		{
			k=0;
		}
		if(res==0)
		{
			if(i==q-1)
				printf("-1\n");
			else
				printf("-1 ");
			continue;
		}
		while(res%3==0||res%5==0||res%2==0)
		{
			if(res%3==0)
			{
				res=(res/3)*2;
				k++;
			}
			if(res%5==0)
			{
				res=(res/5)*4;
				k++;
			}
			if(res%2==0)
			{
				res=(res/2);
				k++;
			}
		}
		if(res==1)
		{
			if(i==q-1)
				printf("%I64d\n",k);
			else
				printf("%I64d ",k);
		}
		else
		{
			if(i==q-1)
				printf("-1\n");
			else
				printf("-1 ");
		}
	}
	return 0;
}


#include <iostream>//http://codeforces.com/contest/1176/problem/A
using namespace std;
int main()
{
	int n;
	cin>>n;
	int sum;
	long long k;
	while(n--)
	{
		cin>>k;
		//cout<<k<<endl;
		sum=0;
		while(1)
		{
			if(k%2==0)
			{
				k/=2;
				sum++;
			}
			else if(k%3==0)
			{
				k/=3;
				k*=2;
				sum++;
			}
			else if(k%5==0)
			{
				k/=5;
				k*=4;
				sum++;
			}
			else
			break;
		}
		if(k==1)
		printf("%d\n",sum);
		else
		printf("-1\n");
	}
	return 0;
}


// divide it.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

using namespace std;

int main()
{
	int q;
	cin >> q;

	while (q--) {
		unsigned long long  n;
		cin >> n;
		int c = 0;

		while (n>0) {
			if (n == 1) {
				cout << c;

				break;

			}

				if (n % 2 == 0) {
					n /= 2;
					if (n == 1) {
						cout << c+1;

						break;

					}
				}

				else if (n % 3 == 0) {
					n = 2 * n / 3;
					if (n == 1) {
						cout << c+1;

						break;

					}
				}
				else if (n % 5 == 0) {
					n = 4 * n / 5;
					if (n == 1) {
						cout << c+1;

						break;

					}
				}
				else if (n % 2 != 0 || n % 3 != 0 || n % 5 != 0){
					cout << -1;
					break;
				}

				c++;
		}

		cout << "\n";
	}
	return 0;

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file


#include<iostream>
using namespace  std;
int main()
{
	int i,q;
	cin>>q;
	for(i=0;i<q;i++)
	{
		long long int n;
		cin>>n;
		if(n==1)
			cout<<'0'<<endl;
		else if(n%5==0||n%2==0||n%3==0)
		{
			int c=0;
			while(n%5==0)
			{
				n=n/5;
				c+=3;
			}
			while(n%3==0)
			{
				n=n/3;
				c+=2;
			}
			while(n%2==0)
			{
				n=n/2;
				c+=1;
			}
			if(n==1)
				cout<<c<<endl;
		    else
		    	cout<<"-1"<<endl;
		}
		else
			cout<<"-1"<<endl;
	}
}


#include<iostream>
//#include<bits/stdc++>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
        int n,cnt=0;
        cin>>n;
        while(n>1)
        {
           if(n%2==0)
           {
               n=n/2;
               cnt++;
           }
           else if(n%3==0)
           {
               n=(2*n)/3;
               cnt++;
           }
           else if(n%5==0)
           {
               n=(4*n)/5;
               cnt++;
           }
           else{
               break;
           }
        }
        cout<<cnt<<"\n";
    }
    return 0;
}


#include<iostream>
//#include<bits/stdc++>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
        int n,cnt=0;
        while(n>1)
        {
           if(n%2==0)
           {
               n=n/2;
               cnt++;
           }
           else if(n%3==0)
           {
               n=(2*n)/3;
               cnt++;
           }
           else if(n%5==0)
           {
               n=(4*n)/5;
               cnt++;
           }
           else{
               break;
           }
        }
        cout<<cnt<<"\n";
    }
    return 0;
}


#include<iostream>
//#include<bits/stdc++>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
       long long int n;
        int cnt=0;
        cin>>n;
        while(n>1)
        {
           if(n%2==0)
           {
               n=n/2;
               cnt++;
           }
           else if(n%3==0)
           {
               n=(2*n)/3;
               cnt++;
           }
           else if(n%5==0)
           {
               n=(4*n)/5;
               cnt++;
           }
           else{
               break;
           }
        }
        if(n>1){
            cnt=-1;
        }
        cout<<cnt<<"\n";
    }
    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
        long long int n;
        int cnt=0;
        cin>>n;
        while(n>1)
        {
            if(n%2==0){
                n=n/2;
                cnt++;
            }
            else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
            else if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
            else{
                break;
            }
        }
        if(n>1){
            cnt=-1;
        }
        cout<<cnt<<"\n";
    }
    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
        long long int n;
        int cnt=0;
        ciin>>n;
        while(n>1)
        {
            if(n%2==0){
                n=n/2;
                cnt++;
            }
            else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
            else if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
            else{
                break;
            }
        }
        if(n>1){
            cnt=-1;
        }
        cout<<cnt<<"\n";
    }
    return 0;

}


#include<iostream>
//#include<bits/stdc++>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
       long long int n;
        int cnt=0;
        cin>>n
        while(n>1)
        {
           if(n%2==0)
           {
               n=n/2;
               cnt++;
           }
           else if(n%3==0)
           {
               n=(2*n)/3;
               cnt++;
           }
           else if(n%5==0)
           {
               n=(4*n)/5;
               cnt++;
           }
           else{
               break;
           }
        }
        if(n>1){
            cnt=-1;
        }
        cout<<cnt<<"\n";
    }
    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {
        long long int n;
        int cnt=0;
        cin>>n;
        while(n>1)
        {
            if(n%2==0){
                n=n/2;
                cnt++;
            }
            else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
            else if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
            else{
                break;
            }
        }
        if(n>1){
            cnt=-1;
        }
        cout<<cnt<<"\n";
    }
    return 0;

}
close


#include  <stdlib.h>
#include  <iostream>
#include  <algorithm>
using namespace std;
int main()
{
	int long long aa;
	int n,i,flag=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>aa;
		flag=0;
		while(aa!=1&&(aa%2==0||aa%3==0||aa%5==0)){
		if(aa%2==0)
		{
			aa=aa/2;
			flag++;
		}
		if(aa%3==0)
		{
			aa=(aa*2)/3;
			flag++;
		}
		if(aa%5==0)
		{
			aa=(aa*4)/5;
			flag++;
		}
		}
		if(aa!=1)
			cout<<"-1"<<endl;
		else
			cout<<flag<<endl;
}
	return 0;

}


#include <cstdio>
#include <iostream>
using namespace std;
long long cnt2, cnt3, cnt5, n;

int main() {
	int q;
	cin >> q;
	while (q--) {
		cnt2 = 0;
		cnt3 = 0;
		cnt5 = 0;
		cin >> n;
		while (n % 2 == 0) {
			n /= 2;
			cnt2++;
		}

		while (n % 3 == 0) {
			n /= 3;
			cnt3++;
		}

		while (n % 5 == 0) {
			n /= 5;
			cnt5++;
		}

		if (n != 1) cout << "-1" <<endl;
		else cout << cnt2 + cnt3 * 2 + cnt5 * 3 << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int a;
	cin>>a;
	for (int i = 0; i < a; ++i) {
		long long n;
		cin>>n;
		int sum = 0, sum2 = 0, sum3 = 0;
		while (n % 2 == 0) {
			n /= 2;
			sum++;
		}
		while (n % 3 == 0) {
			n /= 3;
			sum2++;
		}
		while (n % 5 == 0) {
			n /= 5;
			sum3++;
		}
		if (n != 1) {
			cout << -1 << endl;
		} else {
			cout << sum + sum2 * 2 + sum3 * 3 << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>

typedef unsigned long long ull;

using namespace std;

int main(){

    //Divide it!

    ull n, x;
    int i, q, a, b, c, ans;

    cin>>q;

    for (i=1;i<=q;i++){
        cin>>n;
        a=0;
        b=0;
        c=0;
        x=n;
        while (x%2==0){
            x/=2;
            a++;
        }
        while (x%3==0){
            x/=3;
            b++;
        }
        while (x%5==0){
            x/=5;
            c++;
        }
        if (x==1){
            ans=a+2*b+3*c;
            cout<<ans<<'\n';
        }
        else {
            cout<<"-1\n";
        }
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(){

    //Divide it!

    ull n, x;
    int i, q, a, b, c, ans;

    cin>>q;

    for (i=1;i<=q;i++){
        cin>>n;
        a=0;
        b=0;
        c=0;
        x=n;
        while (x%2==0){
            x/=2;
            a++;
        }
        while (x%3==0){
            x/=3;
            b++;
        }
        while (x%5==0){
            x/=5;
            c++;
        }
        if (x==1){
            ans=a+2*b+3*c;
            cout<<ans<<'\n';
        }
        else {
            cout<<"-1\n";
        }
    }

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
   int q;
   cin >>q;
   int i;
   for(i=0;i<q;i++)
   {
       long long int n;
       int two=0,thr=0,fiv=0;
       cin >>n ;
       if(n==1){cout << 0<<endl;continue;}
       while(n%2==0)
       {
           n=n/2;
           two++;
       }
       while(n%3==0)
       {
           n=n/3;
           thr++;
       }
       while(n%5==0)
       {
           n=n/5;
           fiv++;
       }
       if(n==1)
       {
           cout << (3*fiv + thr*2 + two)<<endl;
       }
       else cout <<-1 <<endl;
   }
   return 0;
}


#include<iostream>
#include <fstream>
#include<string>
using namespace std;
typedef __int64 ll;
void main()
{
	int q;
	cin >> q;
	ll x;
	while (q--)
	{
		cin >> x;
		ll count = 0;
		while (x>1)
		{
			if (x%2==0)
			{
				x /= 2;
				count++;
			}
			else
			{
				if (x%3==0)
				{
					x = 2 * x / 3;
					count++;
				}
				else
				{
					if (x%5==0)
					{
						x = 4 * x / 5;
						count++;
					}
					else
					{
						cout << -1 << endl;
						break;
					}
				}
			}
		}
		if (x<=1)
		{
			cout << count << endl;

		}

	}

//	system("pause");
}


#include<iostream>
#include <fstream>
#include<string>
using namespace std;
typedef __int64 ll;
void main()
{
	int q;
	cin >> q;
	ll x;
	while (q--)
	{
		cin >> x;
		ll count = 0;
		while (x>1)
		{
			if (x%2==0)
			{
				x /= 2;
				count++;
			}
			else
			{
				if (x%3==0)
				{
					x = 2 * x / 3;
					count++;
				}
				else
				{
					if (x%5==0)
					{
						x = 4 * x / 5;
						count++;
					}
					else
					{
						cout << -1 << endl;
						break;
					}
				}
			}
		}
		if (x<=1)
		{
			cout << count << endl;

		}

	}

	system("pause");
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#define ll long long
#define maxn 1e18+5

using namespace std;

ll xiao=maxn;
ll dfs(ll su,ll ans)
{
	if(su==1)
	{
		xiao=min(xiao,ans);
		return 1;
	}
	else if(su%2==0||su%3==0||su%5==0){
		if(su%2==0)
		{
			dfs(su/2,ans+1);
		}
		else if(su%3==0)
		{
			dfs(su/3*2,ans+1);
		}
		else if(su%5==0)
		{
			dfs(su/5*4,ans+1);
		}
	}
	else return 0;
}

int main()
{
	ll n;

	cin>>n;
	for(ll i=0;i<n;i++)
	{
		ll su;
		xiao=maxn;
		cin>>su;
		if(dfs(su,0))
		cout<<xiao<<' ';
		else cout<<-1<<' ';
	}

	return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
/*
Replace n with n/2 if n is divisible by 2;
Replace n with 2n/3 if n is divisible by 3;
Replace n with 4n/5 if n is divisible by 5.
*/
int main()
{
	int n;
	cin>>n;
	vector<unsigned long long>a(n);
	for(int i=0;i<n;i++)
	cin>>a[i];
	int result=0;
	for(int i=0;i<n;i++)
	{
		result=0;
		while(a[i]>1)
		{
			if(a[i]%2==0)
				a[i]/=2;
			else if(a[i]%3==0)
				a[i]=(2*a[i])/3;
			else if(a[i]%5==0)
				a[i]=(4*a[i])/5;
			else
			{
				cout<<-1<<endl;
				goto label;
			}
			result++;
		}
		cout<<result<<endl;
		label:;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
/*
Replace n with n/2 if n is divisible by 2;
Replace n with 2n/3 if n is divisible by 3;
Replace n with 4n/5 if n is divisible by 5.
*/
int main()
{
	int n;
	cin>>n;
	vector<unsigned long long>a(n);
	for(int i=0;i<n;i++)
	cin>>a[i];
	int result=0;
	for(int i=0;i<n;i++)
	{
		result=0;
		while(a[i]>1)
		{
			if(a[i]%2==0)
				a[i]/=2;
			else if(a[i]%3==0)
				a[i]=(2*a[i])/3;
			else if(a[i]%5==0)
				a[i]=(4*a[i])/5;
			else
			{
				cout<<-1<<endl;
				break;
			}
			result++;
		}
		cout<<result<<endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int main() {
    int test=1;
    scanf("%d",&test);
    long long int num;
    for(int t=1; t<=test; t++){

		cin>>num;
        int count=0;

        for(long long int i=num; i>=1; ){

            if(i==1)break;

            if((i%5==0)||(i%3==0)||(i%2==0)){
                if(i%5==0){
                    i=(i*4)/5;
                    count++;
                }

                if(i%3==0){
                    i=(i*2)/3;
                    count++;
                }

                if(i%2==0){
                    i/=2;
                    count++;
                }
            }
            else{
                count=-1;
                break;
            }

        }
        cout<<count<<endl;
    }

    return 0;
}


#include <iostream>

using namespace std;
typedef long long LL;
int main()
{
    int count = 0;
    int T;
    cin >> T;
    LL x;
    while(T --)
    {
        cin >> x;
        bool flag = 0;
        //cout << x << endl;
        count = 0;
        while(1)
        {
            if(x == 1ll)break;
            if(x % 2ll != 0ll && x % 3ll != 0ll && x % 5 != 0ll)
            {
                flag = 1;
                break;
            }
            if(x % 2ll == 0ll)
            {
                x /= 2;
                count ++;
            }else if(x % 3ll == 0ll)
            {
                x /= 3ll;
                x *= 2ll;
                count ++;
            }else{
                x /= 5ll;
                x *= 4ll;
                count ++;
            }
        }
        if(!flag)cout << count << endl;
        else cout << -1 << endl;
    }
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int q;
    cin >> q;
    vector <long long> a(q);
    for (int i = 0; i < q; i++) cin >> a[i];
    for (int i = 0; i < q; i++) {
        if (a[i] == 1) cout << "0" << endl;
        else
        if (a[i] % 2 != 0 && a[i] % 3 != 0 && a[i] % 5 != 0) cout << "-1";
        else {
            int s = 0;
            int k = 0;
            while (a[i] != 1) {
            if (a[i] % 2 == 0) {
                a[i] = a[i] / 2;
                k++;
            }
            else if (a[i] % 3 == 0) {
                a[i] = 2 * a[i] / 3;
                k++;
            }
            else if (a[i] % 5 == 0)  {
                a[i] = 4 * a[i] / 5;
                  k++;
            }
            else {
                a[i] = 1;
                cout << "-1" << endl;
                s++;
                }
            }
            if (s != 1) cout << k << endl;
        }
        }
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int q;
    cin >> q;
    vector <long long> a(q);
    for (int i = 0; i < q; i++) cin >> a[i];
    for (int i = 0; i < q; i++) {
        if (a[i] == 1) cout << "0" << endl;
        else
        if (a[i] % 2 != 0 && a[i] % 3 != 0 && a[i] % 5 != 0) cout << "-1" << endl;
        else {
            int s = 0;
            int k = 0;
            while (a[i] != 1) {
            if (a[i] % 2 == 0) {
                a[i] = a[i] / 2;
                k++;
            }
            else if (a[i] % 3 == 0) {
                a[i] = 2 * a[i] / 3;
                k++;
            }
            else if (a[i] % 5 == 0)  {
                a[i] = 4 * a[i] / 5;
                  k++;
            }
            else {
                a[i] = 1;
                cout << "-1" << endl;
                s++;
                }
            }
            if (s != 1) cout << k << endl;
        }
        }
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int q;
    cin >> q;
    vector <long long> a(q);
    for (int i = 0; i < q; i++) cin >> a[i];
    for (int i = 0; i < q; i++) {
        if (a[i] == 1) cout << "0" << endl;
        else
        if (a[i] % 2 != 0 && a[i] % 3 != 0 && a[i] % 5 != 0) cout << "-1";
        else {
            int k = 0;
            while (a[i] != 1) {
            if (a[i] % 2 == 0) {
                a[i] = a[i] / 2;
                k++;
            }
            else if (a[i] % 3 == 0) {
                a[i] = 2 * a[i] / 3;
                k++;
            }
            else if (a[i] % 5 == 0)  {
                a[i] = 4 * a[i] / 5;
                  k++;
            }
            else {
                a[i] = 1;
                cout << "-1" << endl;
                }
            }
            cout << k << endl;
        }
        }
}


#include <iostream>

using namespace std;

int main() {
	int q;
	cin >> q;
	while(q--){
		long long n;
		cin >> n;
		int flag = 0;
		while(n!=1){
			if(n%2==0){
				n = n / 2;
				flag++;
				continue;
			}
			if(n%3==0){
				n = n / 3 * 2;
				flag++;
				continue;
			}
			if(n%5==0){
				n = n / 5 * 4;
				flag++;
				continue;
			}
			flag = -1;
			break;
		}
		if(q != 0)
			cout << flag << " ";
		else
		    cout << flag << endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
#define ll long long
int main(){
	int t,ans;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		if(n==1){
			cout<<"0";
		}else if(n%2==0||n%3==0||n%5==0){
			int flag=1,sum=0;
			while(n--){
				if(n%2==0){
					n=n/2;
					sum++;
				}
				if(n%3==0){
					n=2*n/3;
					sum++;
				}
				if(n%5){
					n=4*n/5;
					sum++;
				}
				if(n==1) break;
				if(n!=0&&n*4%5!=0&&n%2!=0&&n*2%3!=0){
					flag = 0;
					break;
				}
			}
			if(flag){
				cout<<sum<<endl;;
			}else
				cout<<"-1"<<endl;
		}
		else cout<<"-1"<<endl;
	}
	system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;
ll dfs(ll n,ll step)
{
    if(n==1)
        return step;
    if(n%5==0)
        return dfs(n/5*4,step+1);
    else if(n%3==0)
        return dfs(n/3*2,step+1);
    else if(n%2==0)
        return dfs(n/2,step+1);
    else
        return -1;

}
int main()
{

    int q;
    scanf("%d",&q);
    while(q--)
    {
        ll n;
        scanf("%I64d",&n);
        printf("%I64d ",dfs(n,0));

    }

    return 0;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
	int a;
	vector<long long int> array(2);
	cin >> a;
	array.resize(a);
	int i,count=0;
	for(i=0;i<a;i++){
		cin >> array[i];
	}
	for(i=0;i<a;i++){
		while(array[i]!=1 && array[i]!=-1){
			if(array[i]%2==0){
				count++;
				array[i]=array[i]/2;
			}
			else if(array[i]%3==0){
				array[i]=(2 * array[i])/3;
				count++;
			}
			else if(array[i]%5==0){
				array[i]=(4 * array[i])/5;
				count++;
			}
			else{
				array[i]=-1;
				count= -1;
			}
			//cout << array[i] << endl;
		}
		cout << count << endl;
		count =0;
	}
	return 0;
}


#include<iostream>
#include<string>
using namespace std;
main(){
	int a;
	cin >> a;
	signed long long int array[a];
	int i,count=0;
	for(i=0;i<a;i++){
		cin >> array[i];
	}
	for(i=0;i<a;i++){
		while(array[i]!=1 && array[i]!=-1){
			if(array[i]%2==0){
				count++;
				array[i]=array[i]/2;
			}
			else if(array[i]%3==0){
				array[i]=(2 * array[i])/3;
				count++;
			}
			else if(array[i]%5==0){
				array[i]=(4 * array[i])/5;
				count++;
			}
			else{
				array[i]=-1;
				count= -1;
			}
			//cout << array[i] << endl;
		}
		cout << count << endl;
		count =0;
	}
}


#include<iostream>
#include<string>
using namespace std;
int main(){
	int a;
	cin >> a;
	 int* array=new int[a];
	int i,count=0;
	for(i=0;i<a;i++){
		scanf("%d",array+i);

	}
	for(i=0;i<a;i++){
		while(*(array+i)!=1 && *(array+i)!=-1){
			if(*(array+i)%2==0){
				count++;
				*(array+i)=*(array+i)/2;
			}
			else if(*(array+i)%3==0){
				*(array+i)=(2 * *(array+i))/3;
				count++;
			}
			else if(*(array+i)%5==0){
				*(array+i)=(4 * *(array+i))/5;
				count++;
			}
			else{
				*(array+i)=-1;
				count= -1;
			}
			//cout << array[i] << endl;
		}
		cout << count << endl;
		count =0;
	}
	return 0 ;
}


#include <iostream>

using namespace std;

int main()
{
    long long n = 0;
    int m = 0, l = 0;
    cin >> n;
    while (cin >> n) {
        while (true) {
            while (n % 2 == 0) {
                n /= 2;
                m++;
            }
            while ((2 * n) % 3 == 0) {
                n *= 2;
                n /= 3;
                m++;
            }
            while ((4 * n) % 5 == 0) {
                n *= 4;
                n /= 5;
                m++;
            }
            if (n == 1) {
                cout << m << endl;
                m = 0;
                l = 0;
                break;
            }
            l++;
            if (l > 100) {
                cout << -1 << endl;
                m = 0;
                l = 0;
                break;
            }
        }
    }
}


#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
typedef __int64 ll;
const int maxn=2e5+50;
int main()
{
	int q;
	scanf("%d",&q);
	while(q--)
	{
		ll x,ans=0;
		scanf("%I64d",&x);
		while(x%5==0)
		{
			x=x/5*4;
			ans++;
		}
		while(x%3==0)
		{
			x=x/3*2;
			ans++;
		}
		while(x%2==0)
		{
			x/=2;
			ans++;
		}
		if(x==1)printf("%I64d\n",ans);
		else puts("-1");
	}

}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;

const int maxn=1e5+5;

typedef long long ll;

int main()
{
	int n;
	while(~scanf("%d",&n)){
		while(n--){
			ll x;
			scanf("%lld",&x);
			int flag=0;
			int cnt=0;
			while(1){
				if(x%2==0){
					x/=2;
				}
				else if(x%3==0){
					x=x*2/3;
				}
				else if(x%5==0){
					x=x*4/5;
				}
				else{
					if(x==1){
						flag=1;
					}
					break;
				}
				cnt++;
			}
			if(flag){
				printf("%d\n",cnt);
			}
			else{
				printf("-1\n");
			}
		}

	}
	return 0;
}


import math

# A function to print all prime factors of
# a given number n
def pf(n):
	l=[]
	# Print the number of two's that divide n
	while n % 2 == 0:
		l.append(int(2))
		n = n / 2

	# n must be odd at this point
	# so a skip of 2 ( i = i + 2) can be used
	for i in range(3,int(math.sqrt(n))+1,2):

		# while i divides n , print i ad divide n
		while n % i== 0:
			l.append(int(i))
			n = n // i

	# Condition if n is a prime
	# number greater than 2
	if n > 2:
		l.append(int(n))

	return l
t=int(input())
while t:
    t-=1
    n=int(input())
    if n==1:
        print(0)
    else:
        k=pf(n)
        #print(k)
        kk=list(set(k))
        #print(kk)
        if len(kk) >3:
            print(-1)
            continue
        c=0
        for i in range(len(kk)):
            if kk[i] !=2 and kk[i] !=3 and kk[i] !=5:
                print(-1)
                c=1
                break

        if c:
            continue
        c2=k.count(2)
        c3=k.count(3)
        c5=k.count(5)
        print(c2+(2*c3)+(3*c5))


t=int(input())
while t:
    t-=1
    n=int(input())
    if n==1:
        print(0)
    else:
        ans=0
        c5=0
        c3=0
        while 1 :
            if n%5==0:
                n=n//5
                c5+=1
                ans+=1
            if n%3==0:
                n=n//3
                c3+=1
                ans+=1
            if n%2==0:
                n=n//2
                ans+=1
            if n%5 and n%3 and n%2:
                break
        if n==1:
            print(ans+c3+(2*c5))
        else:
            print(-1)


t=int(input())
while t:
    t-=1
    n=int(input())
    if n==1:
        print(0)
    else:
        ans=0
        c5=0
        c3=0
        while 1 :
            if n%5==0:
                n=n//5
                c5+=1
                ans+=1
            if n%3==0:
                n=n//3
                c3+=1
                ans+=1
            if n%2==0:
                n=n//2
                ans+=1
            if n%5 and n%3 and n%2:
                break
        print(ans+c3+(2*c5))


for i in range(int(input())):
    j=0
    k=0
    a=int(input())
    if a==1:
        print(0)
    else:
        while True:
            if a==1:
                break
            elif (a%5==0 or a%3==0 or a%2==0) and a!=1:
                if a%5==0:
                    a=(4*a)//5
                    j+=1
                elif a%3==0:
                    a=(2*a)//3
                    j+=1
                else:
                    a=a//2
                    j+=1
            else:
                k=1
                print(-1)
                break
        if k==0:
            print(j)


for i in range(int(input())):
    j=0
    a=int(input())
    if a==1:
        print(0)
    elif a%5%3%2!=0 and a%5!=0 and a%3!=0 and a%2!=0:
        print(-1)
    else:
        while True:
            if a==1:
                break
            elif a%5==0:
                a=(4*a)//5
                j+=1
            elif a%3==0:
                a=(2*a)//3
                j+=1
            else:
                a=a//2
                j+=1
        print(j)


for i in range(int(input())):
    j=0
    k=0
    a=int(input())
    if a==1:
        print(0)
    while True:
        if a==1:
            break
        elif (a%5==0 or a%3==0 or a%2==0) and a!=1:
            if a%5==0:
                a=(4*a)//5
                j+=1
            elif a%3==0:
                a=(2*a)//3
                j+=1
            else:
                a=a//2
                j+=1
        else:
            k=1
            print(-1)
            break
    if k==0:
        print(j)


for i in range(int(input())):
    j=0
    a=int(input())
    if a==1:
        print(0)
    elif a%5%3%2!=0:
        print(-1)
    else:
        while True:
            if a==1:
                break
            elif a%5==0:
                a=(4*a)//5
                j+=1
            elif a%3==0:
                a=(2*a)//3
                j+=1
            else:
                a=a//2
                j+=1
        print(j)


#include <bits/stdc++.h>
#define F first
#define S second
#define dq deque
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define itor iterator
#define sint short int
#define ull unsigned ll
#define pii pair<int,int>
#define uint unsigned int
#define All(x) x.begin(),x.end()
#define B cout << "BreakPoint" << endl;
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Fin(_filename) freopen(_filename,"r",stdin)
#define Fout(_filename) freopen(_filename,"w",stdout)
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
#define Fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
void Print(int _x){printf("%d\n",_x);}
void Print(ll _x){printf("%lld\n",_x);}
_tp<_tyn T>void Print( T _a[] , int _s , int _t ){
    for( int i = _s ; i <= _t ; i++ )
        cout << _a[i] << " ";
   	cout << endl;
}
#define il inline
#define rg register
struct InputReader{
    static const int bs = 100000;
    char buf[bs];
    int p;
    il InputReader():p(bs) {}
    il void Flush(){
        p = 0;
        fread(buf, 1, bs, stdin);
    }
    il char C(){
        if(p >= bs) Flush();
        return buf[p++];
    }
    int Getnum(){
        char ch = C();
        while( ch < '0'  ||  ch > '9' ) ch = C();
        return (int)(ch-'0');
    }
    il int operator() (){
        int ans = 0;
        char ch = C();
        int fu = 1;
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    ll Readll(){
        ll ans = 0LL;
        ll fu = 1;
        char ch = C();
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10LL;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    il void Readstring( string &x ){
        x.clear();
        char ch = C();
        while( !isdigit(ch)  &&  !isalpha(ch) ) ch = C();
        while( isdigit(ch)  ||  isalpha(ch) ){
            x += ch;
            ch = C();
        }
        //x += '\0';
    }
    il void Readalpha( char &c ){
        c = C();
        while( !isalpha(c) ) c = C();
    }
    il char Specialread(){
        char ch = C();
        while( !isalpha(ch) ) ch = C();
        return ch;
    }
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){
    x1 = In(); x2 = In(); x3 = In();
}
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){
    x1 = In(); x2 = In(); x3 = In(); x4 = In();
}
il void Read( ll &x ){
    x = In.Readll();
}
il void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll();
}
il void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll();
}
clock_t _starttime , _endtime;
il void FILEIO(){
    #ifdef WIN32
        _starttime = clock();
        Fin("in.in");
    #endif
}
#ifdef WIN32
    #include <windows.h>
    #include <psapi.h>
#endif
il void END(){
    #ifdef WIN32
        _endtime = clock();
        double _timeuse = (_endtime-_starttime)* 1000.0 / CLOCKS_PER_SEC;
        printf("\n\nTime usage:\n%.0lf ms\n",_timeuse);
        HANDLE _handle = GetCurrentProcess();
        PROCESS_MEMORY_COUNTERS pmc;
        GetProcessMemoryInfo(_handle,&pmc,sizeof(pmc));
        double _memuse = pmc.PeakPagefileUsage/1024.00/1024.00;
        printf("Memory usage:\n%.1lf MB\n\n",_memuse);
    #endif
    exit(0);
}
#define For(i,j) for( rg int (i) = 1 ; (i) <= (j) ; (i)++ )
#define For0(i,j) for( rg int (i) = 0 ; (i) < (j) ; (i)++ )
#define Forx(i,j,k) for( rg int (i) = (j) ; (i) <= (k) ; (i)++ )
#define Forstep(i,j,k,st) for( rg int (i) = (j) ; (i) <= (k) ; (i) += (st) )
#define fOR(i,j) for( rg int (i) = (j) ; (i) >= 1 ; (i)-- )
#define fOR0(i,j) for( rg int (i) = (j)-1 ; (i) >= 0 ; (i)-- )
#define fORx(i,j,k) for( rg int (i) = (j) ; (i) >= (k) ; (i)-- )
int dx[] = {0,-1,0,1};
int dy[] = {1,0,-1,0};
//			D L U  R
#define Mymod(a,b) if( a >= b ) a -= b;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 3010

int n,m;
int p[MAXN];
ll c[MAXN];

vc<ll> sel[MAXN];
ll sum[MAXN][MAXN];
vc<ll> allcost;
//ll allsum[MAXN];

int main(){
	FILEIO();
	Ms(sum);
	//Ms(allsum);

	Read(n,m);
	For(i,n){
		Read(p[i]);
		Read(c[i]);
		sel[p[i]].Pb(c[i]);
		//if( p[i] != 1 ) allcost.Pb(c[i]);
	}

	For(i,m){
		if(!sel[i].size()) continue;
		sort(All(sel[i]));
		sum[i][0] = sel[i][0];
		For(j,MAXN-2)
			sum[i][j] = sum[i][j-1] + (j<sel[i].size()?sel[i][j]:0);
	}
	/*
	sort(All(allcost));
	allsum[0] = allcost[0];
	For(j,MAXN-2)
		allsum[j] = allsum[j-1] + (j<allcost.size()?allcost[j]:0);
	*/
//B
	int sel1 = sel[1].size();
	ll ans = LINF;
	Forx(i,sel1,n){
	//int i = 0;
		//O(i);
		ll tans = 0;
		int gain = sel1;
		Forx(j,2,m){
			if( sel[j].size() < i ) continue;
			tans += sum[j][sel[j].size()+1-i-1];
			gain += sel[j].size()+1-i;
		}//O(gain);
		if( gain < i ){
			allcost.clear();
			Forx(j,2,m){
				int st = 0;
				if( sel[j].size() >= i ) st = i-sel[j].size()+1;//O(st);
				//O(sel[j].size());
				if( sel[j].size() <= st ) continue;
				Forx(k,st,sel[j].size()-1){
					//O(sel[j].size()-1);
					allcost.Pb(sel[j][k]);
				}
			}
			sort(All(allcost));
			//For0(j,allcost.size()){
			//	cout << allcost[j] << " ";
			//}cout << endl;
			if( i-gain-1 >= allcost.size() ){
				tans = LINF;
			}else{
				Forx(j,0,i-gain-1)
					tans += allcost[j];
			}
		}
		//O(tans);
		Mymin(ans,tans);
	}
	printf("%lld\n",ans);

	END();
}


#include <bits/stdc++.h>
#define F first
#define S second
#define dq deque
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define itor iterator
#define sint short int
#define ull unsigned ll
#define pii pair<int,int>
#define uint unsigned int
#define All(x) x.begin(),x.end()
#define B cout << "BreakPoint" << endl;
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Fin(_filename) freopen(_filename,"r",stdin)
#define Fout(_filename) freopen(_filename,"w",stdout)
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
#define Fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
void Print(int _x){printf("%d\n",_x);}
void Print(ll _x){printf("%lld\n",_x);}
_tp<_tyn T>void Print( T _a[] , int _s , int _t ){
    for( int i = _s ; i <= _t ; i++ )
        cout << _a[i] << " ";
   	cout << endl;
}
#define il inline
#define rg register
struct InputReader{
    static const int bs = 100000;
    char buf[bs];
    int p;
    il InputReader():p(bs) {}
    il void Flush(){
        p = 0;
        fread(buf, 1, bs, stdin);
    }
    il char C(){
        if(p >= bs) Flush();
        return buf[p++];
    }
    int Getnum(){
        char ch = C();
        while( ch < '0'  ||  ch > '9' ) ch = C();
        return (int)(ch-'0');
    }
    il int operator() (){
        int ans = 0;
        char ch = C();
        int fu = 1;
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    ll Readll(){
        ll ans = 0LL;
        ll fu = 1;
        char ch = C();
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10LL;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    il void Readstring( string &x ){
        x.clear();
        char ch = C();
        while( !isdigit(ch)  &&  !isalpha(ch) ) ch = C();
        while( isdigit(ch)  ||  isalpha(ch) ){
            x += ch;
            ch = C();
        }
        //x += '\0';
    }
    il void Readalpha( char &c ){
        c = C();
        while( !isalpha(c) ) c = C();
    }
    il char Specialread(){
        char ch = C();
        while( !isalpha(ch) ) ch = C();
        return ch;
    }
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){
    x1 = In(); x2 = In(); x3 = In();
}
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){
    x1 = In(); x2 = In(); x3 = In(); x4 = In();
}
il void Read( ll &x ){
    x = In.Readll();
}
il void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll();
}
il void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll();
}
clock_t _starttime , _endtime;
il void FILEIO(){
    #ifdef WIN32
        _starttime = clock();
        Fin("in.in");
    #endif
}
#ifdef WIN32
    #include <windows.h>
    #include <psapi.h>
#endif
il void END(){
    #ifdef WIN32
        _endtime = clock();
        double _timeuse = (_endtime-_starttime)* 1000.0 / CLOCKS_PER_SEC;
        printf("\n\nTime usage:\n%.0lf ms\n",_timeuse);
        HANDLE _handle = GetCurrentProcess();
        PROCESS_MEMORY_COUNTERS pmc;
        GetProcessMemoryInfo(_handle,&pmc,sizeof(pmc));
        double _memuse = pmc.PeakPagefileUsage/1024.00/1024.00;
        printf("Memory usage:\n%.1lf MB\n\n",_memuse);
    #endif
    exit(0);
}
#define For(i,j) for( rg int (i) = 1 ; (i) <= (j) ; (i)++ )
#define For0(i,j) for( rg int (i) = 0 ; (i) < (j) ; (i)++ )
#define Forx(i,j,k) for( rg int (i) = (j) ; (i) <= (k) ; (i)++ )
#define Forstep(i,j,k,st) for( rg int (i) = (j) ; (i) <= (k) ; (i) += (st) )
#define fOR(i,j) for( rg int (i) = (j) ; (i) >= 1 ; (i)-- )
#define fOR0(i,j) for( rg int (i) = (j)-1 ; (i) >= 0 ; (i)-- )
#define fORx(i,j,k) for( rg int (i) = (j) ; (i) >= (k) ; (i)-- )
int dx[] = {0,-1,0,1};
int dy[] = {1,0,-1,0};
//			D L U  R
#define Mymod(a,b) if( a >= b ) a -= b;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 3010

int n,m;
int p[MAXN];
ll c[MAXN];

vc<ll> sel[MAXN];
ll sum[MAXN][MAXN];
vc<ll> allcost;
//ll allsum[MAXN];

int main(){
	FILEIO();
	Ms(sum);
	//Ms(allsum);

	Read(n,m);
	For(i,n){
		Read(p[i]);
		Read(c[i]);
		sel[p[i]].Pb(c[i]);
		//if( p[i] != 1 ) allcost.Pb(c[i]);
	}

	For(i,m){
		if(!sel[i].size()) continue;
		sort(All(sel[i]));
		sum[i][0] = sel[i][0];
		For(j,MAXN-2)
			sum[i][j] = sum[i][j-1] + (j<(int)(sel[j].size())?sel[i][j]:0);
	}
	/*
	sort(All(allcost));
	allsum[0] = allcost[0];
	For(j,MAXN-2)
		allsum[j] = allsum[j-1] + (j<allcost.size()?allcost[j]:0);
	*/
//B
	int sel1 = (int)(sel[1].size());
	ll ans = LINF;
	Forx(i,sel1,m){
	//int i = 0;
		//O(i);
		ll tans = 0;
		int gain = sel1;
		Forx(j,2,m){
			if( (int)(sel[j].size()) < i ) continue;
			tans += sum[j][(int)(sel[j].size())-i+1-1];
			gain += (int)(sel[j].size())-i+1;
		}//O(gain);
		if( gain < i ){
			allcost.clear();
			Forx(j,2,m){
				int st = 0;
				if( (int)(sel[j].size()) >= i ) st = i-(int)(sel[j].size())+1;//O(st);
				//O(sel[j].size());
				Forx(k,st,(int)(sel[j].size())-1){
					//O(sel[j].size()-1);
					allcost.Pb(sel[j][k]);
				}
			}
			sort(All(allcost));
			//For0(j,allcost.size()){
			//	cout << allcost[j] << " ";
			//}cout << endl;
			if( i-gain-1 >= (int)(allcost.size()) ){
				tans = LINF;
			}else{
				Forx(j,0,i-gain-1)
					tans += allcost[j];
			}
		}
		//O(tans);
		Mymin(ans,tans);
	}
	printf("%lld\n",ans);

	END();
}


#include <bits/stdc++.h>
#define F first
#define S second
#define dq deque
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define itor iterator
#define sint short int
#define ull unsigned ll
#define pii pair<int,int>
#define uint unsigned int
#define All(x) x.begin(),x.end()
#define B cout << "BreakPoint" << endl;
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Fin(_filename) freopen(_filename,"r",stdin)
#define Fout(_filename) freopen(_filename,"w",stdout)
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
#define Fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
void Print(int _x){printf("%d\n",_x);}
void Print(ll _x){printf("%lld\n",_x);}
_tp<_tyn T>void Print( T _a[] , int _s , int _t ){
    for( int i = _s ; i <= _t ; i++ )
        cout << _a[i] << " ";
   	cout << endl;
}
#define il inline
#define rg register
struct InputReader{
    static const int bs = 100000;
    char buf[bs];
    int p;
    il InputReader():p(bs) {}
    il void Flush(){
        p = 0;
        fread(buf, 1, bs, stdin);
    }
    il char C(){
        if(p >= bs) Flush();
        return buf[p++];
    }
    int Getnum(){
        char ch = C();
        while( ch < '0'  ||  ch > '9' ) ch = C();
        return (int)(ch-'0');
    }
    il int operator() (){
        int ans = 0;
        char ch = C();
        int fu = 1;
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    ll Readll(){
        ll ans = 0LL;
        ll fu = 1;
        char ch = C();
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10LL;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    il void Readstring( string &x ){
        x.clear();
        char ch = C();
        while( !isdigit(ch)  &&  !isalpha(ch) ) ch = C();
        while( isdigit(ch)  ||  isalpha(ch) ){
            x += ch;
            ch = C();
        }
        //x += '\0';
    }
    il void Readalpha( char &c ){
        c = C();
        while( !isalpha(c) ) c = C();
    }
    il char Specialread(){
        char ch = C();
        while( !isalpha(ch) ) ch = C();
        return ch;
    }
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){
    x1 = In(); x2 = In(); x3 = In();
}
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){
    x1 = In(); x2 = In(); x3 = In(); x4 = In();
}
il void Read( ll &x ){
    x = In.Readll();
}
il void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll();
}
il void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll();
}
clock_t _starttime , _endtime;
il void FILEIO(){
    #ifdef WIN32
        _starttime = clock();
        Fin("in.in");
    #endif
}
#ifdef WIN32
    #include <windows.h>
    #include <psapi.h>
#endif
il void END(){
    #ifdef WIN32
        _endtime = clock();
        double _timeuse = (_endtime-_starttime)* 1000.0 / CLOCKS_PER_SEC;
        printf("\n\nTime usage:\n%.0lf ms\n",_timeuse);
        HANDLE _handle = GetCurrentProcess();
        PROCESS_MEMORY_COUNTERS pmc;
        GetProcessMemoryInfo(_handle,&pmc,sizeof(pmc));
        double _memuse = pmc.PeakPagefileUsage/1024.00/1024.00;
        printf("Memory usage:\n%.1lf MB\n\n",_memuse);
    #endif
    exit(0);
}
#define For(i,j) for( rg int (i) = 1 ; (i) <= (j) ; (i)++ )
#define For0(i,j) for( rg int (i) = 0 ; (i) < (j) ; (i)++ )
#define Forx(i,j,k) for( rg int (i) = (j) ; (i) <= (k) ; (i)++ )
#define Forstep(i,j,k,st) for( rg int (i) = (j) ; (i) <= (k) ; (i) += (st) )
#define fOR(i,j) for( rg int (i) = (j) ; (i) >= 1 ; (i)-- )
#define fOR0(i,j) for( rg int (i) = (j)-1 ; (i) >= 0 ; (i)-- )
#define fORx(i,j,k) for( rg int (i) = (j) ; (i) >= (k) ; (i)-- )
int dx[] = {0,-1,0,1};
int dy[] = {1,0,-1,0};
//			D L U  R
#define Mymod(a,b) if( a >= b ) a -= b;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 3010

int n,m;
int p[MAXN];
ll c[MAXN];

vc<ll> sel[MAXN];
ll sum[MAXN][MAXN];
vc<ll> allcost;
//ll allsum[MAXN];

int main(){
	FILEIO();
	Ms(sum);
	//Ms(allsum);

	Read(n,m);
	For(i,n){
		Read(p[i]);
		Read(c[i]);
		sel[p[i]].Pb(c[i]);
		//if( p[i] != 1 ) allcost.Pb(c[i]);
	}

	For(i,m){
		if(!sel[i].size()) continue;
		sort(All(sel[i]));
		sum[i][0] = sel[i][0];
		For(j,MAXN-2)
			sum[i][j] = sum[i][j-1] + (j<sel[i].size()?sel[i][j]:0);
	}
	/*
	sort(All(allcost));
	allsum[0] = allcost[0];
	For(j,MAXN-2)
		allsum[j] = allsum[j-1] + (j<allcost.size()?allcost[j]:0);
	*/
//B
	int sel1 = sel[1].size();
	ll ans = LINF;
	Forx(i,sel1,n){
	//int i = 0;
		//O(i);
		ll tans = 0;
		int gain = sel1;
		Forx(j,2,m){
			if( sel[j].size() < i ) continue;
			tans += sum[j][sel[j].size()+1-i-1];
			gain += sel[j].size()+1-i;
		}//O(gain);
		if( gain < i ){
			allcost.clear();
			Forx(j,2,m){
				int st = 0;
				if( sel[j].size() >= i ) st = sel[j].size()-i+1;//O(st);
				//O(sel[j].size());
				if( sel[j].size() <= st ) continue;
				Forx(k,st,sel[j].size()-1){
					//O(sel[j].size()-1);
					allcost.Pb(sel[j][k]);
				}
			}
			sort(All(allcost));
			//For0(j,allcost.size()){
			//	cout << allcost[j] << " ";
			//}cout << endl;
			if( i-gain-1 >= allcost.size() ){
				tans = LINF;
			}else{
				Forx(j,0,i-gain-1)
					tans += allcost[j];
			}
		}
		//O(tans);
		Mymin(ans,tans);
	}
	printf("%lld\n",ans);

	END();
}


#include <bits/stdc++.h>
#define F first
#define S second
#define dq deque
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define itor iterator
#define sint short int
#define ull unsigned ll
#define pii pair<int,int>
#define uint unsigned int
#define All(x) x.begin(),x.end()
#define B cout << "BreakPoint" << endl;
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Fin(_filename) freopen(_filename,"r",stdin)
#define Fout(_filename) freopen(_filename,"w",stdout)
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
#define Fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
void Print(int _x){printf("%d\n",_x);}
void Print(ll _x){printf("%lld\n",_x);}
_tp<_tyn T>void Print( T _a[] , int _s , int _t ){
    for( int i = _s ; i <= _t ; i++ )
        cout << _a[i] << " ";
   	cout << endl;
}
#define il inline
#define rg register
struct InputReader{
    static const int bs = 100000;
    char buf[bs];
    int p;
    il InputReader():p(bs) {}
    il void Flush(){
        p = 0;
        fread(buf, 1, bs, stdin);
    }
    il char C(){
        if(p >= bs) Flush();
        return buf[p++];
    }
    int Getnum(){
        char ch = C();
        while( ch < '0'  ||  ch > '9' ) ch = C();
        return (int)(ch-'0');
    }
    il int operator() (){
        int ans = 0;
        char ch = C();
        int fu = 1;
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    ll Readll(){
        ll ans = 0LL;
        ll fu = 1;
        char ch = C();
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10LL;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    il void Readstring( string &x ){
        x.clear();
        char ch = C();
        while( !isdigit(ch)  &&  !isalpha(ch) ) ch = C();
        while( isdigit(ch)  ||  isalpha(ch) ){
            x += ch;
            ch = C();
        }
        //x += '\0';
    }
    il void Readalpha( char &c ){
        c = C();
        while( !isalpha(c) ) c = C();
    }
    il char Specialread(){
        char ch = C();
        while( !isalpha(ch) ) ch = C();
        return ch;
    }
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){
    x1 = In(); x2 = In(); x3 = In();
}
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){
    x1 = In(); x2 = In(); x3 = In(); x4 = In();
}
il void Read( ll &x ){
    x = In.Readll();
}
il void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll();
}
il void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll();
}
clock_t _starttime , _endtime;
il void FILEIO(){
    #ifdef WIN32
        _starttime = clock();
        Fin("in.in");
    #endif
}
#ifdef WIN32
    #include <windows.h>
    #include <psapi.h>
#endif
il void END(){
    #ifdef WIN32
        _endtime = clock();
        double _timeuse = (_endtime-_starttime)* 1000.0 / CLOCKS_PER_SEC;
        printf("\n\nTime usage:\n%.0lf ms\n",_timeuse);
        HANDLE _handle = GetCurrentProcess();
        PROCESS_MEMORY_COUNTERS pmc;
        GetProcessMemoryInfo(_handle,&pmc,sizeof(pmc));
        double _memuse = pmc.PeakPagefileUsage/1024.00/1024.00;
        printf("Memory usage:\n%.1lf MB\n\n",_memuse);
    #endif
    exit(0);
}
#define For(i,j) for( rg int (i) = 1 ; (i) <= (j) ; (i)++ )
#define For0(i,j) for( rg int (i) = 0 ; (i) < (j) ; (i)++ )
#define Forx(i,j,k) for( rg int (i) = (j) ; (i) <= (k) ; (i)++ )
#define Forstep(i,j,k,st) for( rg int (i) = (j) ; (i) <= (k) ; (i) += (st) )
#define fOR(i,j) for( rg int (i) = (j) ; (i) >= 1 ; (i)-- )
#define fOR0(i,j) for( rg int (i) = (j)-1 ; (i) >= 0 ; (i)-- )
#define fORx(i,j,k) for( rg int (i) = (j) ; (i) >= (k) ; (i)-- )
int dx[] = {0,-1,0,1};
int dy[] = {1,0,-1,0};
//			D L U  R
#define Mymod(a,b) if( a >= b ) a -= b;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 3010

int n,m;
int p[MAXN];
ll c[MAXN];

vc<ll> sel[MAXN];
ll sum[MAXN][MAXN];
vc<ll> allcost;
//ll allsum[MAXN];

int main(){
	FILEIO();
	Ms(sum);
	//Ms(allsum);

	Read(n,m);
	For(i,n){
		Read(p[i]);
		Read(c[i]);
		sel[p[i]].Pb(c[i]);
		//if( p[i] != 1 ) allcost.Pb(c[i]);
	}

	For(i,m){
		if(!sel[i].size()) continue;
		sort(All(sel[i]));
		sum[i][0] = sel[i][0];
		For(j,MAXN-2)
			sum[i][j] = sum[i][j-1] + (j<sel[i].size()?sel[i][j]:0);
	}
	/*
	sort(All(allcost));
	allsum[0] = allcost[0];
	For(j,MAXN-2)
		allsum[j] = allsum[j-1] + (j<allcost.size()?allcost[j]:0);
	*/
//B
	int sel1 = sel[1].size();
	ll ans = LINF;
	Forx(i,sel1,m){
	//int i = 0;
		//O(i);
		ll tans = 0;
		int gain = sel1;
		Forx(j,2,m){
			if( sel[j].size() < i ) continue;
			tans += sum[j][sel[j].size()-i+1-1];
			gain += sel[j].size()-i+1;
		}//O(gain);
		if( gain < i ){
			allcost.clear();
			Forx(j,2,m){
				int st = 0;
				if( sel[j].size() >= i ) st = i-sel[j].size()+1;//O(st);
				//O(sel[j].size());
				Forx(k,st,(int)(sel[j].size())-1){
					//O(sel[j].size()-1);
					allcost.Pb(sel[j][k]);
				}
			}
			sort(All(allcost));
			//For0(j,allcost.size()){
			//	cout << allcost[j] << " ";
			//}cout << endl;
			if( i-gain-1 >= allcost.size() ){
				tans = LINF;
			}else{
				Forx(j,0,i-gain-1)
					tans += allcost[j];
			}
		}
		//O(tans);
		Mymin(ans,tans);
	}
	printf("%lld\n",ans);

	END();
}


#include <bits/stdc++.h>
#define F first
#define S second
#define dq deque
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define itor iterator
#define sint short int
#define ull unsigned ll
#define pii pair<int,int>
#define uint unsigned int
#define All(x) x.begin(),x.end()
#define B cout << "BreakPoint" << endl;
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Fin(_filename) freopen(_filename,"r",stdin)
#define Fout(_filename) freopen(_filename,"w",stdout)
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
#define Fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define O_(x) cout << #x << " " << x << "  ";
#define O(x) cout << #x << " " << x << endl;
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
void Print(int _x){printf("%d\n",_x);}
void Print(ll _x){printf("%lld\n",_x);}
_tp<_tyn T>void Print( T _a[] , int _s , int _t ){
    for( int i = _s ; i <= _t ; i++ )
        cout << _a[i] << " ";
   	cout << endl;
}
#define il inline
#define rg register
struct InputReader{
    static const int bs = 100000;
    char buf[bs];
    int p;
    il InputReader():p(bs) {}
    il void Flush(){
        p = 0;
        fread(buf, 1, bs, stdin);
    }
    il char C(){
        if(p >= bs) Flush();
        return buf[p++];
    }
    int Getnum(){
        char ch = C();
        while( ch < '0'  ||  ch > '9' ) ch = C();
        return (int)(ch-'0');
    }
    il int operator() (){
        int ans = 0;
        char ch = C();
        int fu = 1;
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    ll Readll(){
        ll ans = 0LL;
        ll fu = 1;
        char ch = C();
        while( ch < '0'  ||  ch > '9' ){
            if( ch == '-' ) fu = -1;
            ch = C();
        }
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10LL;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
    il void Readstring( string &x ){
        x.clear();
        char ch = C();
        while( !isdigit(ch)  &&  !isalpha(ch) ) ch = C();
        while( isdigit(ch)  ||  isalpha(ch) ){
            x += ch;
            ch = C();
        }
        //x += '\0';
    }
    il void Readalpha( char &c ){
        c = C();
        while( !isalpha(c) ) c = C();
    }
    il char Specialread(){
        char ch = C();
        while( !isalpha(ch) ) ch = C();
        return ch;
    }
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){
    x1 = In(); x2 = In(); x3 = In();
}
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){
    x1 = In(); x2 = In(); x3 = In(); x4 = In();
}
il void Read( ll &x ){
    x = In.Readll();
}
il void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll();
}
il void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){
    x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll();
}
clock_t _starttime , _endtime;
il void FILEIO(){
    #ifdef WIN32
        _starttime = clock();
        Fin("in.in");
    #endif
}
#ifdef WIN32
    #include <windows.h>
    #include <psapi.h>
#endif
il void END(){
    #ifdef WIN32
        _endtime = clock();
        double _timeuse = (_endtime-_starttime)* 1000.0 / CLOCKS_PER_SEC;
        printf("\n\nTime usage:\n%.0lf ms\n",_timeuse);
        HANDLE _handle = GetCurrentProcess();
        PROCESS_MEMORY_COUNTERS pmc;
        GetProcessMemoryInfo(_handle,&pmc,sizeof(pmc));
        double _memuse = pmc.PeakPagefileUsage/1024.00/1024.00;
        printf("Memory usage:\n%.1lf MB\n\n",_memuse);
    #endif
    exit(0);
}
#define For(i,j) for( rg int (i) = 1 ; (i) <= (j) ; (i)++ )
#define For0(i,j) for( rg int (i) = 0 ; (i) < (j) ; (i)++ )
#define Forx(i,j,k) for( rg int (i) = (j) ; (i) <= (k) ; (i)++ )
#define Forstep(i,j,k,st) for( rg int (i) = (j) ; (i) <= (k) ; (i) += (st) )
#define fOR(i,j) for( rg int (i) = (j) ; (i) >= 1 ; (i)-- )
#define fOR0(i,j) for( rg int (i) = (j)-1 ; (i) >= 0 ; (i)-- )
#define fORx(i,j,k) for( rg int (i) = (j) ; (i) >= (k) ; (i)-- )
int dx[] = {0,-1,0,1};
int dy[] = {1,0,-1,0};
//			D L U  R
#define Mymod(a,b) if( a >= b ) a -= b;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 3010

int n,m;
int p[MAXN];
ll c[MAXN];

vc<ll> sel[MAXN];
ll sum[MAXN][MAXN];
vc<ll> allcost;
//ll allsum[MAXN];

int main(){
	FILEIO();
	Ms(sum);
	//Ms(allsum);

	Read(n,m);
	For(i,n){
		Read(p[i]);
		Read(c[i]);
		sel[p[i]].Pb(c[i]);
		//if( p[i] != 1 ) allcost.Pb(c[i]);
	}

	For(i,m){
		if(!sel[i].size()) continue;
		sort(All(sel[i]));
		sum[i][0] = sel[i][0];
		For(j,MAXN-2)
			sum[i][j] = sum[i][j-1] + (j<sel[i].size()?sel[i][j]:0);
	}
	/*
	sort(All(allcost));
	allsum[0] = allcost[0];
	For(j,MAXN-2)
		allsum[j] = allsum[j-1] + (j<allcost.size()?allcost[j]:0);
	*/

	int sel1 = sel[1].size();
	ll ans = LINF;
	Forx(i,sel1,m){
	//int i = 0;
		//O(i);
		ll tans = 0;
		int gain = 0;
		Forx(j,2,m){
			if( sel[j].size() < i ) continue;
			tans += sum[j][sel[j].size()-i+1-1];
			gain += sel[j].size()-i+1;
		}//O(gain);
		if( gain < i ){
			allcost.clear();
			Forx(j,2,m){
				int st = 0;
				if( sel[j].size() >= i ) st = i-sel[j].size()+1;
				Forx(k,st,sel[j].size()-1)
					allcost.Pb(sel[j][k]);
			}
			sort(All(allcost));
			//For0(j,allcost.size()){
			//	cout << allcost[j] << " ";
			//}cout << endl;
			if( i-gain-1 >= allcost.size() ){
				tans = LINF;
			}else{
				Forx(j,0,i-gain-1)
					tans += allcost[j];
			}
		}
		//O(tans);
		Mymin(ans,tans);
	}
	printf("%lld\n",ans);

	END();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3001;
int n, m;
vector<ll> party[N];
int cnt[N];
int sz;
vector<ll> values;
map<ll, int> mep;
void init()
{
	for(ll j : values)
	mep[j]++;
	memset(cnt, 0, sizeof cnt);
	sz = party[1].size();
}
int main()
{
	cin >> n >> m;
	for(int i=0; i<n; i++)
	{
		ll x, y;
		cin >> x >> y;
		if(x != 1)
		values.push_back(y);
		party[x].push_back(y);
	}
	for(int i=1; i<=m; i++)
	sort(party[i].begin(), party[i].end());
	for(int i=2; i<=m; i++)
	{
		if(party[i].size() >= party[1].size())
		break;
		else if(i == m)
		{
			cout << 0 << endl;
			return 0;
		}
	}
	sort(values.begin(), values.end());
	ll mn = 1e18;
	for(int i=party[1].size()+1; i<=n; i++)
	{
		init();
		ll cur = 0;
		for(int j=2; j<=m; j++)
		{
			while(party[j].size() - cnt[j] >= i)
			cur += party[j][cnt[j]], mep[party[j][cnt[j]]]--, cnt[j]++, sz++;
		}
		for(int j : values)
		if(mep[j] > 0 && sz < i)
		mep[j]--, sz++, cur += j;
		mn = min(mn, cur);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3001;
int n, m;
vector<ll> party[N];
int cnt[N];
int sz;
vector<ll> values;
unordered_map<ll, int> mep;
void init()
{
	mep.clear();
	for(ll j : values)
	mep[j]++;
	memset(cnt, 0, sizeof cnt);
	sz = party[1].size();
}
int main()
{
	cin >> n >> m;
	for(int i=0; i<n; i++)
	{
		ll x, y;
		cin >> x >> y;
		if(x != 1)
		values.push_back(y);
		party[x].push_back(y);
	}
	if(m == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	for(int i=1; i<=m; i++)
	sort(party[i].begin(), party[i].end());
	for(int i=2; i<=m; i++)
	{
		if(party[i].size() >= party[1].size())
		break;
		else if(i == m)
		{
			cout << 0 << endl;
			return 0;
		}
	}
	sort(values.begin(), values.end());
	ll mn = 1e18;
	for(int i=party[1].size()+1; i<=n; i++)
	{
		init();
		ll cur = 0;
		for(int j=2; j<=m; j++)
		{
			while(party[j].size() - cnt[j] >= i)
			cur += party[j][cnt[j]], mep[party[j][cnt[j]]]--, cnt[j]++, sz++;
		}
		for(int j : values)
		if(mep[j] > 0 && sz < i)
		mep[j]--, sz++, cur += j;
		mn = min(mn, cur);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3001;
int n, m;
vector<ll> party[N];
int cnt[N];
int sz;
vector<ll> values;
map<ll, int> mep;
void init()
{
	for(ll j : values)
	mep[j]++;
	memset(cnt, 0, sizeof cnt);
	sz = party[1].size();
}
int main()
{
	cin >> n >> m;
	for(int i=0; i<n; i++)
	{
		ll x, y;
		cin >> x >> y;
		if(x != 1)
		values.push_back(y);
		party[x].push_back(y);
	}
	if(m == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	for(int i=1; i<=m; i++)
	sort(party[i].begin(), party[i].end());
	for(int i=2; i<=m; i++)
	{
		if(party[i].size() >= party[1].size())
		break;
		else if(i == m)
		{
			cout << 0 << endl;
			return 0;
		}
	}
	sort(values.begin(), values.end());
	ll mn = 1e18;
	for(int i=party[1].size()+1; i<=n; i++)
	{
		init();
		ll cur = 0;
		for(int j=2; j<=m; j++)
		{
			while(party[j].size() - cnt[j] >= i)
			cur += party[j][cnt[j]], mep[party[j][cnt[j]]]--, cnt[j]++, sz++;
		}
		for(int j : values)
		if(mep[j] > 0 && sz < i)
		mep[j]--, sz++, cur += j;
		mn = min(mn, cur);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3001;
int n, m;
vector<ll> party[N];
int cnt[N];
int sz;
vector<ll> values;
map<ll, int> mep;
void init()
{
	for(ll j : values)
	mep[j]++;
	memset(cnt, 0, sizeof cnt);
	sz = party[1].size();
}
int main()
{
	cin >> n >> m;
	for(int i=0; i<n; i++)
	{
		ll x, y;
		cin >> x >> y;
		values.push_back(y);
		party[x].push_back(y);
	}
	for(int i=1; i<=m; i++)
	sort(party[i].begin(), party[i].end());
	for(int i=2; i<=m; i++)
	{
		if(party[i].size() >= party[1].size())
		break;
		else if(i == m)
		{
			cout << 0 << endl;
			return 0;
		}
	}
	sort(values.begin(), values.end());
	ll mn = 1e18;
	for(int i=party[1].size()+1; i<=n; i++)
	{
		init();
		ll cur = 0;
		for(int j=2; j<=m; j++)
		{
			while(party[j].size() - cnt[j] >= i)
			cur += party[j][cnt[j]], mep[party[j][cnt[j]]]--, cnt[j]++, sz++;
		}
		for(int j : values)
		if(mep[j] > 0 && sz < i)
		mep[j]--, sz++, cur += j;
		mn = min(mn, cur);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3001;
int n, m;
vector<ll> party[N];
int cnt[N];
int sz;
vector<ll> values;
map<ll, int> mep;
void init()
{
	for(ll j : values)
	mep[j]++;
	memset(cnt, 0, sizeof cnt);
	sz = party[1].size();
}
int main()
{
	cin >> n >> m;
	for(int i=0; i<n; i++)
	{
		ll x, y;
		cin >> x >> y;
		if(x != 1)
		values.push_back(y);
		party[x].push_back(y);
	}
	if(m == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	for(int i=1; i<=m; i++)
	sort(party[i].begin(), party[i].end());
	for(int i=2; i<=m; i++)
	{
		if(party[i].size() >= party[1].size())
		break;
		else if(i == m)
		{
			cout << 0 << endl;
			return 0;
		}
	}
	sort(values.begin(), values.end());
	ll mn = 1e18;
	for(int i=party[1].size()+1; i<=n; i++)
	{
		init();
		ll cur = 0;
		for(int j=2; j<=m; j++)
		{
			while(party[j].size() - cnt[j] >= i)
			cur += party[j][cnt[j]], mep[party[j][cnt[j]]]--, cnt[j]++, sz++;
		}
		for(int j : values)
		if(mep[j] > 0 && sz < i)
		mep[j]--, sz++, cur += j;
		for(int j : values)
		mep[j] = 0;
		mn = min(mn, cur);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3001;
int n, m;
vector<ll> party[N];
int cnt[N];
int sz;
vector<ll> values;
map<ll, int> mep;
void init()
{
	mep.clear();
	for(ll j : values)
	mep[j]++;
	memset(cnt, 0, sizeof cnt);
	sz = party[1].size();
}
int main()
{
	cin >> n >> m;
	for(int i=0; i<n; i++)
	{
		ll x, y;
		cin >> x >> y;
		if(x != 1)
		values.push_back(y);
		party[x].push_back(y);
	}
	if(m == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	for(int i=1; i<=m; i++)
	sort(party[i].begin(), party[i].end());
	for(int i=2; i<=m; i++)
	{
		if(party[i].size() >= party[1].size())
		break;
		else if(i == m)
		{
			cout << 0 << endl;
			return 0;
		}
	}
	sort(values.begin(), values.end());
	ll mn = 1e18;
	for(int i=party[1].size()+1; i<=n; i++)
	{
		init();
		ll cur = 0;
		for(int j=2; j<=m; j++)
		{
			while(party[j].size() - cnt[j] >= i)
			cur += party[j][cnt[j]], mep[party[j][cnt[j]]]--, cnt[j]++, sz++;
		}
		for(int j : values)
		if(mep[j] > 0 && sz < i)
		mep[j]--, sz++, cur += j;
		mn = min(mn, cur);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long int
ll n,m;
const int maxn=5e3+5;
long long int p[maxn],c[maxn];
ll total[maxn];
bool vis[maxn];
bool used[maxn];
typedef struct node
{
    long long int f,c;
    bool operator <(const node &r)const
    {
        return c<r.c;
    }
} node;
typedef struct node1
{
    long long int p,f,c;
    bool operator <(const node1 &r)const
    {
        return c<r.c;
    }
} node1;
long long int now[maxn];
vector<node>V[maxn];
vector<node1>use;
ll usenext=0;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> p[i] >> c[i];
        node tmp;
        tmp.f=i,tmp.c=c[i];
        V[p[i]].push_back(tmp);
        total[p[i]]++;
        node1 tmp1;
        tmp1.p=p[i],tmp1.f=i,tmp1.c=c[i];
        if(p[i]!=1)
            use.push_back(tmp1);
        else
            used[i]=1;
    }
    for(int i=1; i<=m; i++)
        sort(V[i].begin(),V[i].end());
    sort(use.begin(),use.end());
    ll nowmax=0;
    vector<ll>nowp;
    nowp.clear();
    for(int i=1; i<=m; i++)
    {
        if(nowmax<total[i])
        {
            nowp.clear();
            memset(vis,0,sizeof(vis));
            nowmax=total[i],nowp.push_back(i);
            vis[i]=1;
        }
        else if(nowmax==total[i])
        {
            nowp.push_back(i);
            vis[i]=1;
        }
    }
    if(nowp.size()==1 && nowp[0]==1)
    {
        cout << 0 << endl;
        return 0;
    }
    long long int ans=0;
    while(total[1]<=nowmax)
    {
        if(nowp.size()+total[1]<=nowmax)
        {
            long long int tmp1=0,tmp2=0,fix=0;
            for(int i=0; i<nowp.size(); i++)
            {
                ll tmpp=nowp[i];
                while(used[V[tmpp][now[tmpp]].f])
                    now[tmpp]++;
                tmp1+=V[tmpp][now[tmpp]].c;
                while(used[use[usenext+i+fix].f])
                    fix++;
                tmp2+=use[usenext+i+fix].c;
            }
            for(int i=0;i<2;i++)
            {
                while(used[use[usenext+fix].f])
                    fix++;
                tmp2+=use[usenext+fix].c;
            }
            if(tmp1<tmp2)
            {
                for(int i=0; i<nowp.size(); i++)
                {
                    ll tmpp=nowp[i];
                    used[V[tmpp][now[tmpp]].f]=1;
                    now[tmpp]++;
                    total[tmpp]--;
                }
                total[1]+=nowp.size();
                nowmax--;
                for(int i=1; i<=n; i++)
                {
                    if(!vis[i] && total[i]==nowmax)
                    {
                        vis[i]=1;
                        nowp.push_back(i);
                    }
                }
                ans+=tmp1;
            }
            else
            {
//                int totalnow=0,fix=0,l=nowp.size();
//                while(totalnow<2)
//                {
//                    while(used[use[usenext+fix].f])
//                        fix++;
//                    total[use[usenext+fix].p]--;
//                    if(vis[use[usenext+fix].p])
//                    {
//                        vis[use[usenext+fix].p]=0;
//                        for(int i=0; i<nowp.size(); i++)
//                        {
//                            if(nowp[i]==use[usenext+fix].p)
//                            {
//                                nowp.erase(nowp.begin()+i);
//                                break;
//                            }
//                        }
//                    }
//                    used[use[usenext+fix].f]=1;
//                    fix++;
//                    totalnow++;
//                }
//                usenext+=fix;
//                ans+=tmp2;
//                total[1]+=2;
                int fix=0;
                while(used[use[usenext+fix].f])
                    fix++;
                total[1]++;
                total[use[usenext+fix].p]--;
                ans+=use[usenext+fix].c;
                used[use[usenext+fix].f]=1;
                if(vis[use[usenext+fix].p])
                {
                    vis[use[usenext+fix].p]=0;
                    for(int i=0; i<nowp.size(); i++)
                    {
                        if(nowp[i]==use[usenext+fix].p)
                        {
                            nowp.erase(nowp.begin()+i);
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            ll fix=0;
            while(used[use[usenext+fix].f])
                fix++;
            total[1]++;
            total[use[usenext+fix].p]--;
            ans+=use[usenext+fix].c;
            used[use[usenext+fix].f]=1;
            if(vis[use[usenext+fix].p])
            {
                vis[use[usenext+fix].p]=0;
                for(int i=0; i<nowp.size(); i++)
                {
                    if(nowp[i]==use[usenext+fix].p)
                    {
                        nowp.erase(nowp.begin()+i);
                        break;
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long int
ll n,m;
const int maxn=5e3+5;
long long int p[maxn],c[maxn];
ll total[maxn];
bool vis[maxn];
bool used[maxn];
typedef struct node
{
    long long int f,c;
    bool operator <(const node &r)const
    {
        return c<r.c;
    }
} node;
typedef struct node1
{
    long long int p,f,c;
    bool operator <(const node1 &r)const
    {
        return c<r.c;
    }
} node1;
//long long int now[maxn];
vector<node>V[maxn];
vector<node1>use;
//ll usenext=0;
//int main()
//{
//    ios::sync_with_stdio(false);
//    cin >> n >> m;
//    for(int i=1; i<=n; i++)
//    {
//        cin >> p[i] >> c[i];
//        node tmp;
//        tmp.f=i,tmp.c=c[i];
//        V[p[i]].push_back(tmp);
//        total[p[i]]++;
//        node1 tmp1;
//        tmp1.p=p[i],tmp1.f=i,tmp1.c=c[i];
//        if(p[i]!=1)
//            use.push_back(tmp1);
//        else
//            used[i]=1;
//    }
//    for(int i=1; i<=m; i++)
//        sort(V[i].begin(),V[i].end());
//    sort(use.begin(),use.end());
//    ll nowmax=0;
//    vector<ll>nowp;
//    nowp.clear();
//    for(int i=1; i<=m; i++)
//    {
//        if(nowmax<total[i])
//        {
//            nowp.clear();
//            memset(vis,0,sizeof(vis));
//            nowmax=total[i],nowp.push_back(i);
//            vis[i]=1;
//        }
//        else if(nowmax==total[i])
//        {
//            nowp.push_back(i);
//            vis[i]=1;
//        }
//    }
//    if(nowp.size()==1 && nowp[0]==1)
//    {
//        cout << 0 << endl;
//        return 0;
//    }
//    long long int ans=0;
//    while(total[1]<=nowmax)
//    {
//        if(nowp.size()+total[1]<=nowmax)
//        {
//            long long int tmp1=0,tmp2=0,fix=0;
//            for(int i=0; i<nowp.size(); i++)
//            {
//                ll tmpp=nowp[i];
//                while(used[V[tmpp][now[tmpp]].f])
//                    now[tmpp]++;
//                tmp1+=V[tmpp][now[tmpp]].c;
//                while(used[use[usenext+i+fix].f])
//                    fix++;
//                tmp2+=use[usenext+i+fix].c;
//            }
//            for(int i=0; i<2; i++)
//            {
//                while(used[use[usenext+fix].f])
//                    fix++;
//                tmp2+=use[usenext+fix].c;
//            }
//            if(tmp1<tmp2)
//            {
//                for(int i=0; i<nowp.size(); i++)
//                {
//                    ll tmpp=nowp[i];
//                    used[V[tmpp][now[tmpp]].f]=1;
//                    now[tmpp]++;
//                    total[tmpp]--;
//                }
//                total[1]+=nowp.size();
//                nowmax--;
//                for(int i=1; i<=m; i++)
//                {
//                    if(!vis[i] && total[i]==nowmax)
//                    {
//                        vis[i]=1;
//                        nowp.push_back(i);
//                    }
//                }
//                ans+=tmp1;
//            }
//            else
//            {
////                int totalnow=0,fix=0,l=nowp.size();
////                while(totalnow<2)
////                {
////                    while(used[use[usenext+fix].f])
////                        fix++;
////                    total[use[usenext+fix].p]--;
////                    if(vis[use[usenext+fix].p])
////                    {
////                        vis[use[usenext+fix].p]=0;
////                        for(int i=0; i<nowp.size(); i++)
////                        {
////                            if(nowp[i]==use[usenext+fix].p)
////                            {
////                                nowp.erase(nowp.begin()+i);
////                                break;
////                            }
////                        }
////                    }
////                    used[use[usenext+fix].f]=1;
////                    fix++;
////                    totalnow++;
////                }
////                usenext+=fix;
////                ans+=tmp2;
////                total[1]+=2;
//                int fix=0;
//                while(used[use[usenext+fix].f])
//                    fix++;
//                total[1]++;
//                total[use[usenext+fix].p]--;
//                ans+=use[usenext+fix].c;
//                used[use[usenext+fix].f]=1;
//                if(vis[use[usenext+fix].p])
//                {
//                    vis[use[usenext+fix].p]=0;
//                    for(int i=0; i<nowp.size(); i++)
//                    {
//                        if(nowp[i]==use[usenext+fix].p)
//                        {
//                            nowp.erase(nowp.begin()+i);
//                            break;
//                        }
//                    }
//                }
//                usenext+=fix;
//            }
//        }
//        else
//        {
//            ll fix=0;
//            while(used[use[usenext+fix].f])
//                fix++;
//            total[1]++;
//            total[use[usenext+fix].p]--;
//            ans+=use[usenext+fix].c;
//            used[use[usenext+fix].f]=1;
//            if(vis[use[usenext+fix].p])
//            {
//                vis[use[usenext+fix].p]=0;
//                for(int i=0; i<nowp.size(); i++)
//                {
//                    if(nowp[i]==use[usenext+fix].p)
//                    {
//                        nowp.erase(nowp.begin()+i);
//                        break;
//                    }
//                }
//            }
//            usenext+=fix;
//        }
//    }
//    cout << ans << endl;
//    return 0;
//}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> p[i] >> c[i];
        if(p[i]!=1)
        {
            node1 tmp;
            tmp.f=i,tmp.p=p[i],tmp.c=c[i];
            use.push_back(tmp);
            node tmp1;
            tmp1.f=i,tmp1.c=c[i];
            V[p[i]].push_back(tmp1);
        }
        else
            used[i]=1;
        total[p[i]]++;
    }
    for(int i=2;i<=m;i++)
        sort(V[i].begin(),V[i].end());
    sort(use.begin(),use.end());
    ll now[maxn];
    ll out=0x7fffffffffffffff;
    for(int i=total[1];i<=n;i++)
    {
        if(i==0)    continue;
        bool visnow[maxn];
        memcpy(now,total,sizeof(total));
        memcpy(visnow,used,sizeof(vis));
        ll ans=0;
        for(int j=2;j<=m;j++)
        {
            if(now[j]>=i)
            {
                for(int x=0;x<now[j]-i+1;x++)
                {
                    ans+=V[j][x].c;
                    visnow[V[j][x].f]=1;
                }
                now[1]+=now[j]-i+1;
                now[j]=i-1;
            }
        }
        if(now[1]>i)
            continue;
        int d=i-now[1];
        int fix=0;
        bool flag=1;
        for(int j=0;j<d;j++)
        {
            while(j+fix<use.size() && visnow[use[j+fix].f])
                fix++;
            if(j+fix==use.size())
            {
                flag=0;
                break;
            }
            ans+=use[j+fix].c;
            now[use[j+fix].p]--;
            visnow[use[j+fix].f]=1;
        }
        for(int j=2;j<=m;j++)
        {
            if(now[j]>=i)
            {
                flag=0;
                break;
            }
        }
        if(flag)
        {
            out=min(out,ans);
            continue;
        }
    }
    cout << out << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n,m;
const int maxn=3e3+5;
long long int p[maxn],c[maxn];
int total[maxn];
bool vis[maxn];
bool used[maxn];
typedef struct node
{
    int f,c;
    bool operator <(const node &r)const
    {
        return c<r.c;
    }
} node;
typedef struct node1
{
    int p,f,c;
    bool operator <(const node1 &r)const
    {
        return c<r.c;
    }
} node1;
int now[maxn];
vector<node>V[maxn];
vector<node1>use;
int usenext=0;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> p[i] >> c[i];
        node tmp;
        tmp.f=i,tmp.c=c[i];
        V[p[i]].push_back(tmp);
        total[p[i]]++;
        node1 tmp1;
        tmp1.p=p[i],tmp1.f=i,tmp1.c=c[i];
        if(p[i]!=1)
            use.push_back(tmp1);
        else
            used[i]=1;
    }
    for(int i=1; i<=n; i++)
        sort(V[i].begin(),V[i].end());
    sort(use.begin(),use.end());
    int nowmax=0;
    vector<int>nowp;
    nowp.clear();
    for(int i=1; i<=n; i++)
    {
        if(nowmax<total[i])
        {
            nowp.clear();
            memset(vis,0,sizeof(vis));
            nowmax=total[i],nowp.push_back(i);
            vis[i]=1;
        }
        else if(nowmax==total[i])
        {
            nowp.push_back(i);
            vis[i]=1;
        }
    }
    if(nowp.size()==1 && nowp[0]==1)
    {
        cout << 0 << endl;
        return 0;
    }
    int ans=0;
    while(total[1]<=nowmax)
    {
        if(nowp.size()+total[1]<=nowmax)
        {
            int tmp1=0,tmp2=0,fix=0;
            for(int i=0; i<nowp.size(); i++)
            {
                int tmpp=nowp[i];
                while(used[V[tmpp][now[tmpp]].f])
                    now[tmpp]++;
                tmp1+=V[tmpp][now[tmpp]].c;
                while(used[use[usenext+i+fix].f])
                    fix++;
                tmp2+=use[usenext+i+fix].c;
            }
            for(int i=0;i<2;i++)
            {
                while(used[use[usenext+fix].f])
                    fix++;
                tmp2+=use[usenext+fix].c;
            }
            if(tmp1<tmp2)
            {
                for(int i=0; i<nowp.size(); i++)
                {
                    int tmpp=nowp[i];
                    used[V[tmpp][now[tmpp]].f]=1;
                    now[tmpp]++;
                    total[tmpp]--;
                }
                total[1]+=nowp.size();
                nowmax--;
                for(int i=1; i<=n; i++)
                {
                    if(!vis[i] && total[i]==nowmax)
                    {
                        vis[i]=1;
                        nowp.push_back(i);
                    }
                }
                ans+=tmp1;
            }
            else
            {
//                int totalnow=0,fix=0,l=nowp.size();
//                while(totalnow<2)
//                {
//                    while(used[use[usenext+fix].f])
//                        fix++;
//                    total[use[usenext+fix].p]--;
//                    if(vis[use[usenext+fix].p])
//                    {
//                        vis[use[usenext+fix].p]=0;
//                        for(int i=0; i<nowp.size(); i++)
//                        {
//                            if(nowp[i]==use[usenext+fix].p)
//                            {
//                                nowp.erase(nowp.begin()+i);
//                                break;
//                            }
//                        }
//                    }
//                    used[use[usenext+fix].f]=1;
//                    fix++;
//                    totalnow++;
//                }
//                usenext+=fix;
//                ans+=tmp2;
//                total[1]+=2;
                int fix=0;
            while(used[use[usenext+fix].f])
                fix++;
            total[1]++;
            total[use[usenext+fix].p]--;
            ans+=use[usenext+fix].c;
            used[use[usenext+fix].f]=1;
            if(vis[use[usenext+fix].p])
            {
                vis[use[usenext+fix].p]=0;
                for(int i=0; i<nowp.size(); i++)
                {
                    if(nowp[i]==use[usenext+fix].p)
                    {
                        nowp.erase(nowp.begin()+i);
                        break;
                    }
                }
            }
            }
        }
        else
        {
            int fix=0;
            while(used[use[usenext+fix].f])
                fix++;
            total[1]++;
            total[use[usenext+fix].p]--;
            ans+=use[usenext+fix].c;
            used[use[usenext+fix].f]=1;
            if(vis[use[usenext+fix].p])
            {
                vis[use[usenext+fix].p]=0;
                for(int i=0; i<nowp.size(); i++)
                {
                    if(nowp[i]==use[usenext+fix].p)
                    {
                        nowp.erase(nowp.begin()+i);
                        break;
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n,m;
const int maxn=3e3+5;
long long int p[maxn],c[maxn];
int total[maxn];
bool vis[maxn];
bool used[maxn];
typedef struct node
{
    long long int f,c;
    bool operator <(const node &r)const
    {
        return c<r.c;
    }
} node;
typedef struct node1
{
    long long int p,f,c;
    bool operator <(const node1 &r)const
    {
        return c<r.c;
    }
} node1;
long long int now[maxn];
vector<node>V[maxn];
vector<node1>use;
int usenext=0;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> p[i] >> c[i];
        node tmp;
        tmp.f=i,tmp.c=c[i];
        V[p[i]].push_back(tmp);
        total[p[i]]++;
        node1 tmp1;
        tmp1.p=p[i],tmp1.f=i,tmp1.c=c[i];
        if(p[i]!=1)
            use.push_back(tmp1);
        else
            used[i]=1;
    }
    for(int i=1; i<=n; i++)
        sort(V[i].begin(),V[i].end());
    sort(use.begin(),use.end());
    int nowmax=0;
    vector<int>nowp;
    nowp.clear();
    for(int i=1; i<=n; i++)
    {
        if(nowmax<total[i])
        {
            nowp.clear();
            memset(vis,0,sizeof(vis));
            nowmax=total[i],nowp.push_back(i);
            vis[i]=1;
        }
        else if(nowmax==total[i])
        {
            nowp.push_back(i);
            vis[i]=1;
        }
    }
    if(nowp.size()==1 && nowp[0]==1)
    {
        cout << 0 << endl;
        return 0;
    }
    int ans=0;
    while(total[1]<=nowmax)
    {
        if(nowp.size()+total[1]<=nowmax)
        {
            long long int tmp1=0,tmp2=0,fix=0;
            for(int i=0; i<nowp.size(); i++)
            {
                int tmpp=nowp[i];
                while(used[V[tmpp][now[tmpp]].f])
                    now[tmpp]++;
                tmp1+=V[tmpp][now[tmpp]].c;
                while(used[use[usenext+i+fix].f])
                    fix++;
                tmp2+=use[usenext+i+fix].c;
            }
            for(int i=0;i<2;i++)
            {
                while(used[use[usenext+fix].f])
                    fix++;
                tmp2+=use[usenext+fix].c;
            }
            if(tmp1<tmp2)
            {
                for(int i=0; i<nowp.size(); i++)
                {
                    int tmpp=nowp[i];
                    used[V[tmpp][now[tmpp]].f]=1;
                    now[tmpp]++;
                    total[tmpp]--;
                }
                total[1]+=nowp.size();
                nowmax--;
                for(int i=1; i<=n; i++)
                {
                    if(!vis[i] && total[i]==nowmax)
                    {
                        vis[i]=1;
                        nowp.push_back(i);
                    }
                }
                ans+=tmp1;
            }
            else
            {
//                int totalnow=0,fix=0,l=nowp.size();
//                while(totalnow<2)
//                {
//                    while(used[use[usenext+fix].f])
//                        fix++;
//                    total[use[usenext+fix].p]--;
//                    if(vis[use[usenext+fix].p])
//                    {
//                        vis[use[usenext+fix].p]=0;
//                        for(int i=0; i<nowp.size(); i++)
//                        {
//                            if(nowp[i]==use[usenext+fix].p)
//                            {
//                                nowp.erase(nowp.begin()+i);
//                                break;
//                            }
//                        }
//                    }
//                    used[use[usenext+fix].f]=1;
//                    fix++;
//                    totalnow++;
//                }
//                usenext+=fix;
//                ans+=tmp2;
//                total[1]+=2;
                int fix=0;
                while(used[use[usenext+fix].f])
                    fix++;
                total[1]++;
                total[use[usenext+fix].p]--;
                ans+=use[usenext+fix].c;
                used[use[usenext+fix].f]=1;
                if(vis[use[usenext+fix].p])
                {
                    vis[use[usenext+fix].p]=0;
                    for(int i=0; i<nowp.size(); i++)
                    {
                        if(nowp[i]==use[usenext+fix].p)
                        {
                            nowp.erase(nowp.begin()+i);
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            int fix=0;
            while(used[use[usenext+fix].f])
                fix++;
            total[1]++;
            total[use[usenext+fix].p]--;
            ans+=use[usenext+fix].c;
            used[use[usenext+fix].f]=1;
            if(vis[use[usenext+fix].p])
            {
                vis[use[usenext+fix].p]=0;
                for(int i=0; i<nowp.size(); i++)
                {
                    if(nowp[i]==use[usenext+fix].p)
                    {
                        nowp.erase(nowp.begin()+i);
                        break;
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n,m;
const int maxn=5e3+5;
long long int p[maxn],c[maxn];
int total[maxn];
bool vis[maxn];
bool used[maxn];
typedef struct node
{
    long long int f,c;
    bool operator <(const node &r)const
    {
        return c<r.c;
    }
} node;
typedef struct node1
{
    long long int p,f,c;
    bool operator <(const node1 &r)const
    {
        return c<r.c;
    }
} node1;
long long int now[maxn];
vector<node>V[maxn];
vector<node1>use;
int usenext=0;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> p[i] >> c[i];
        node tmp;
        tmp.f=i,tmp.c=c[i];
        V[p[i]].push_back(tmp);
        total[p[i]]++;
        node1 tmp1;
        tmp1.p=p[i],tmp1.f=i,tmp1.c=c[i];
        if(p[i]!=1)
            use.push_back(tmp1);
        else
            used[i]=1;
    }
    for(int i=1; i<=n; i++)
        sort(V[i].begin(),V[i].end());
    sort(use.begin(),use.end());
    int nowmax=0;
    vector<int>nowp;
    nowp.clear();
    for(int i=1; i<=n; i++)
    {
        if(nowmax<total[i])
        {
            nowp.clear();
            memset(vis,0,sizeof(vis));
            nowmax=total[i],nowp.push_back(i);
            vis[i]=1;
        }
        else if(nowmax==total[i])
        {
            nowp.push_back(i);
            vis[i]=1;
        }
    }
    if(nowp.size()==1 && nowp[0]==1)
    {
        cout << 0 << endl;
        return 0;
    }
    long long int ans=0;
    while(total[1]<=nowmax)
    {
        if(nowp.size()+total[1]<=nowmax)
        {
            long long int tmp1=0,tmp2=0,fix=0;
            for(int i=0; i<nowp.size(); i++)
            {
                int tmpp=nowp[i];
                while(used[V[tmpp][now[tmpp]].f])
                    now[tmpp]++;
                tmp1+=V[tmpp][now[tmpp]].c;
                while(used[use[usenext+i+fix].f])
                    fix++;
                tmp2+=use[usenext+i+fix].c;
            }
            for(int i=0;i<2;i++)
            {
                while(used[use[usenext+fix].f])
                    fix++;
                tmp2+=use[usenext+fix].c;
            }
            if(tmp1<tmp2)
            {
                for(int i=0; i<nowp.size(); i++)
                {
                    int tmpp=nowp[i];
                    used[V[tmpp][now[tmpp]].f]=1;
                    now[tmpp]++;
                    total[tmpp]--;
                }
                total[1]+=nowp.size();
                nowmax--;
                for(int i=1; i<=n; i++)
                {
                    if(!vis[i] && total[i]==nowmax)
                    {
                        vis[i]=1;
                        nowp.push_back(i);
                    }
                }
                ans+=tmp1;
            }
            else
            {
//                int totalnow=0,fix=0,l=nowp.size();
//                while(totalnow<2)
//                {
//                    while(used[use[usenext+fix].f])
//                        fix++;
//                    total[use[usenext+fix].p]--;
//                    if(vis[use[usenext+fix].p])
//                    {
//                        vis[use[usenext+fix].p]=0;
//                        for(int i=0; i<nowp.size(); i++)
//                        {
//                            if(nowp[i]==use[usenext+fix].p)
//                            {
//                                nowp.erase(nowp.begin()+i);
//                                break;
//                            }
//                        }
//                    }
//                    used[use[usenext+fix].f]=1;
//                    fix++;
//                    totalnow++;
//                }
//                usenext+=fix;
//                ans+=tmp2;
//                total[1]+=2;
                int fix=0;
                while(used[use[usenext+fix].f])
                    fix++;
                total[1]++;
                total[use[usenext+fix].p]--;
                ans+=use[usenext+fix].c;
                used[use[usenext+fix].f]=1;
                if(vis[use[usenext+fix].p])
                {
                    vis[use[usenext+fix].p]=0;
                    for(int i=0; i<nowp.size(); i++)
                    {
                        if(nowp[i]==use[usenext+fix].p)
                        {
                            nowp.erase(nowp.begin()+i);
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            int fix=0;
            while(used[use[usenext+fix].f])
                fix++;
            total[1]++;
            total[use[usenext+fix].p]--;
            ans+=use[usenext+fix].c;
            used[use[usenext+fix].f]=1;
            if(vis[use[usenext+fix].p])
            {
                vis[use[usenext+fix].p]=0;
                for(int i=0; i<nowp.size(); i++)
                {
                    if(nowp[i]==use[usenext+fix].p)
                    {
                        nowp.erase(nowp.begin()+i);
                        break;
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}