#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;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNkZWZpbmUgbm1heCAzMDEwCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG4sIG07CmludCBwW25tYXhdLCBjW25tYXhdLCB2b3Rlc1tubWF4XSwgYmFja3VwVm90ZXNbbm1heF07CgptdWx0aXNldCA8aW50PiBzdFtubWF4XSwgYmFja3VwW25tYXhdOwoKaW50IG1haW4oKQp7CiAgICBzY2FuZigiJWQgJWQiLCAmbiwgJm0pOwoKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewoKICAgICAgICBzY2FuZigiJWQgJWQiLCAmcFtpXSwgJmNbaV0pOwoKICAgICAgICBzdFtwW2ldXS5pbnNlcnQoY1tpXSk7IHZvdGVzW3BbaV1dKys7CiAgICB9CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbTsgaSsrKSB7CgogICAgICAgIGJhY2t1cFtpXSA9IHN0W2ldOwogICAgICAgIGJhY2t1cFZvdGVzW2ldID0gdm90ZXNbaV07CiAgICB9CgogICAgbG9uZyBsb25nIGludCBhbnN3ZXIgPSAxZTE4OwoKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewoKICAgICAgICAvL3BhcnR5IDEgd2lucyB3aXRoIChpKSB2b3RlcwoKICAgICAgICBsb25nIGxvbmcgaW50IGN1cl9hbnN3ZXIgPSAwOwoKICAgICAgICBmb3IgKGludCBqID0gMjsgaiA8PSBtOyBqKyspCiAgICAgICAgICAgIHdoaWxlICh2b3Rlc1tqXSA+PSBpKSB7CgogICAgICAgICAgICAgICAgY3VyX2Fuc3dlciArPSAqc3Rbal0uYmVnaW4oKTsKCiAgICAgICAgICAgICAgICBzdFtqXS5lcmFzZShzdFtqXS5iZWdpbigpKTsKCiAgICAgICAgICAgICAgICB2b3Rlc1tqXS0tOyB2b3Rlc1sxXSsrOwoKICAgICAgICAgICAgfQoKICAgICAgICBpZiAodm90ZXNbMV0gPj0gaSkgYW5zd2VyID0gbWluKGFuc3dlciwgY3VyX2Fuc3dlcik7IGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGludCBkaWYgPSBpIC0gdm90ZXNbMV07CiAgICAgICAgICAgIHZlY3RvciA8aW50PiBjdXJfYXJyYXk7CgogICAgICAgICAgICBmb3IgKGludCBqID0gMjsgaiA8PSBtOyBqKyspCiAgICAgICAgICAgICAgICBmb3IgKHNldDxpbnQ+OjppdGVyYXRvciBpdCA9IHN0W2pdLmJlZ2luKCk7IGl0ICE9IHN0W2pdLmVuZCgpOyBpdCsrKQogICAgICAgICAgICAgICAgICAgIGN1cl9hcnJheS5wdXNoX2JhY2soKml0KTsKCiAgICAgICAgICAgIHNvcnQoY3VyX2FycmF5LmJlZ2luKCksIGN1cl9hcnJheS5lbmQoKSk7CgogICAgICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBkaWY7IGorKykgY3VyX2Fuc3dlciArPSBjdXJfYXJyYXlbaiAtIDFdOwoKICAgICAgICAgICAgYW5zd2VyID0gbWluKGFuc3dlciwgY3VyX2Fuc3dlcik7CiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBtOyBqKyspIHsKCiAgICAgICAgICAgIHN0W2pdID0gYmFja3VwW2pdOwogICAgICAgICAgICB2b3Rlc1tqXSA9IGJhY2t1cFZvdGVzW2pdOwogICAgICAgIH0KICAgIH0KCiAgICBwcmludGYoIiVJNjRkIiwgYW5zd2VyKTsKCiAgICByZXR1cm4gMDsKfQo=