#include <stdio.h>
#include <math.h>
#include <set>
#include <vector>
#include <algorithm>
#define nmax 3010

using namespace std;

int n, m;
int p[nmax], c[nmax], votes[nmax], backupVotes[nmax];

multiset <int> st[nmax], backup[nmax];

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; i++) {

        scanf("%d %d", &p[i], &c[i]);

        st[p[i]].insert(c[i]); votes[p[i]]++;
    }

    for (int i = 1; i <= m; i++) {

        backup[i] = st[i];
        backupVotes[i] = votes[i];
    }

    long long int answer = 1e18;

    for (int i = 1; i <= n; i++) {

        //party 1 wins with (i) votes

        long long int cur_answer = 0;

        for (int j = 2; j <= m; j++)
            while (votes[j] >= i) {

                cur_answer += *st[j].begin();

                st[j].erase(st[j].begin());

                votes[j]--; votes[1]++;

            }

        if (votes[1] >= i) answer = min(answer, cur_answer); else
        {
            int dif = i - votes[1];
            vector <int> cur_array;

            for (int j = 2; j <= m; j++)
                for (set<int>::iterator it = st[j].begin(); it != st[j].end(); it++)
                    cur_array.push_back(*it);

            sort(cur_array.begin(), cur_array.end());

            for (int j = 1; j <= dif; j++) cur_answer += cur_array[j - 1];

            answer = min(answer, cur_answer);
        }

        for (int j = 1; j <= m; j++) {

            st[j] = backup[j];
            votes[j] = backupVotes[j];
        }
    }

    printf("%I64d", answer);

    return 0;
}
