#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3005;
struct Voter {
  int p, cost;
  bool buy;
  Voter(int P, int C) : p(P), cost(C){};
};
vector<Voter *> v[MAXN];
bool cmp(Voter *o1, Voter *o2) { return o1->cost < o2->cost; }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int P, C;
    cin >> P >> C;
    Voter *vv = new Voter(P, C);
    v[P].push_back(vv);
    if (P != 1) v[0].push_back(vv);
  }
  int beg = v[1].size();
  for (int i = 0; i <= m; ++i) sort(v[i].begin(), v[i].end(), cmp);
  long long ans = 1e15;
  for (int fin = max(beg, 1); fin <= n; fin++) {
    for (int i = 0; i < v[0].size(); ++i) v[0][i]->buy = 0;
    long long C = 0;
    int get = beg;
    for (int k = 2; k <= m; ++k) {
      if (v[k].size() >= fin) {
        int add = v[k].size() - fin + 1;
        for (int i = 0; i < add; ++i) {
          C += v[k][i]->cost;
          v[k][i]->buy = 1;
        }
        get += add;
      }
    }
    for (int i = 0;; i++) {
      if (get >= fin) break;
      if (v[0][i]->buy) continue;
      C += v[0][i]->cost, get++;
    }
    ans = min(ans, C);
  }
  cout << ans << endl;
}
