#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
const signed long long llf = (signed long long)(~0ull >> 1);
typedef class Voter {
 public:
  int to, v, id;
  Voter() {}
  Voter(int to, int v, int id) : to(to), v(v), id(id) {}
  bool operator<(Voter b) const { return v < b.v; }
} Voter;
Voter ar[N];
int n, m;
long long res = llf;
vector<Voter> vs[N];
bool sec[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int p, v;
    scanf("%d%d", &p, &v);
    vs[p].push_back(Voter(p, v, i));
    ar[i] = Voter(p, v, i);
  }
  sort(ar + 1, ar + n + 1);
  for (int i = 1; i <= m; i++) sort(vs[i].begin(), vs[i].end());
  for (int p1 = vs[1].size(); p1 <= n; p1++) {
    long long cmp = 0;
    int got = vs[1].size();
    memset(sec, 0, sizeof(sec));
    for (int i = 2; i <= m; i++) {
      int s = vs[i].size() - p1 + 1;
      for (int j = 0; j < s && j < vs[i].size(); j++)
        cmp += vs[i][j].v, got++, sec[vs[i][j].id] = 1;
    }
    int p = 1;
    while (got < p1) {
      if (!sec[ar[p].id] && ar[p].to > 1) {
        cmp += ar[p].v;
        sec[ar[p].id] = true;
        got++;
      }
      p++;
    }
    res = min(res, cmp);
  }
  printf("%lld", res);
  return 0;
}
