#include <bits/stdc++.h>
using namespace std;
struct Petya {
  int voice;
  int price;
};
vector<Petya> p;
vector<bool> used;
vector<int> counter;
int n, m, ourself;
long long answer;
bool comp(Petya x, Petya y) { return (x.price > y.price); }
int main() {
  answer = 100000000000000;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    Petya petya;
    scanf("%d %d", &petya.voice, &petya.price);
    if (petya.voice == 1) {
      ourself++;
      continue;
    }
    p.push_back(petya);
  }
  used.resize(n + 1);
  counter.resize(m + 1);
  sort(p.begin(), p.end(), comp);
  for (int rest = 1; rest <= n + 1; rest++) {
    long long cash = 0;
    int voices = 0;
    fill(used.begin(), used.end(), false);
    fill(counter.begin(), counter.end(), 0);
    for (int i = 0; i < p.size(); i++) {
      if (counter[p[i].voice] + 1 < rest) {
        counter[p[i].voice]++;
      } else {
        voices++;
        cash += p[i].price;
        used[i] = true;
      }
    }
    int i = p.size() - 1;
    while (i >= 0 && voices + ourself < rest) {
      if (!used[i]) {
        voices++;
        cash += p[i].price;
      }
      i--;
    }
    if (voices + ourself >= rest) {
      answer = min(answer, cash);
    }
  }
  printf("%lld\n", answer);
  return 0;
}
