#include <bits/stdc++.h>
const double Pi = acos(-1.0);
using namespace std;
int n, m;
int cnt[3010];
vector<pair<long long, long long> > cost;
int main(int argc, char** argv) {
  scanf("%d %d", &n, &m);
  int p, c;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &p, &c);
    if (p != 1)
      cost.push_back(pair<long long, long long>(c, p));
    else
      cnt[1]++;
  }
  if (cnt[1] > n / 2) {
    puts("0");
    return 0;
  }
  sort(cost.begin(), cost.end());
  long long ans = LLONG_MAX;
  for (int i = 0; i <= n / 2; i++) {
    int tmp = cnt[1];
    for (int j = 2; j <= m; j++) cnt[j] = 0;
    long long sum = 0;
    bool used[3010] = {0};
    for (int j = cost.size() - 1; j >= 0; j--) {
      int pj = cost[j].second;
      if (cnt[pj] < i)
        cnt[pj]++;
      else {
        sum += cost[j].first;
        tmp++;
        used[j] = true;
      }
    }
    if (tmp <= i) {
      for (int j = 0; j < (int)cost.size(); j++) {
        if (used[j]) continue;
        sum += cost[j].first;
        if (++tmp > i) break;
      }
    }
    if (tmp > i) ans = min(ans, sum);
  }
  printf("%lld\n", ans);
  return 0;
}
