#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 5;
const long long INF = 1e18;
struct Person {
  long long p;
  int c, id;
  friend bool operator<(Person a, Person b) { return a.p < b.p; }
} p[maxn];
int n, m, fl[maxn];
vector<Person> g[maxn];
long long solve(int goal) {
  long long ans = 0;
  memset(fl, 0, sizeof(fl));
  int cnt = g[1].size();
  for (int i = 2; i <= m; i++) {
    if (g[i].size() < goal) continue;
    for (int j = 0; g[i].size() - j >= goal; j++) {
      ans += g[i][j].p, fl[g[i][j].id] = 1, cnt++;
    }
  }
  if (cnt < goal) {
    int pos = 1;
    while (cnt < goal && pos <= n) {
      if (fl[pos] || p[pos].c == 1) {
        pos++;
        continue;
      }
      pos++, cnt++, ans += p[pos - 1].p;
    }
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%I64d", &p[i].c, &p[i].p);
  sort(p + 1, p + 1 + n);
  for (int i = 1; i <= n; i++) p[i].id = i;
  for (int i = 1; i <= n; i++) g[p[i].c].push_back(p[i]);
  long long ans = INF;
  for (int i = max((int)g[1].size(), 1); i <= n; i++) ans = min(ans, solve(i));
  printf("%I64d\n", ans);
  return 0;
}
