#include <bits/stdc++.h>
using namespace std;
struct Node {
  long long id;
  long long to;
  long long mon;
  bool used;
};
struct Org {
  long long tot;
};
Org org[3005];
Node all[3005];
bool cmp(Node x, Node y) { return x.mon < y.mon; }
long long org_last[3005];
int main() {
  long long m, n;
  cin >> m >> n;
  Node t;
  long long minn = 0x7fffffffffffffff;
  for (int i = 1; i <= m; i++) {
    scanf("%lld %lld", &t.to, &t.mon);
    t.id = i;
    org[t.to].tot++;
    all[i] = t;
  }
  sort(all + 1, all + m + 1, cmp);
  long long maxn = m / 2 + m % 2;
  if (org[1].tot > maxn) {
    minn = 0;
  }
  for (int i = org[1].tot; i <= maxn; i++) {
    bool ok = true;
    long long sum = 0;
    int now = org[1].tot;
    for (int j = 1; j <= m; j++) {
      all[j].used = false;
    }
    for (int j = 2; j <= n; j++) {
      org_last[j] = min(org[j].tot - i + 1, org[j].tot);
    }
    for (int j = 1; j <= m; j++) {
      if (org_last[all[j].to] > 0) {
        now++;
        org_last[all[j].to]--;
        all[j].used = true;
        sum += all[j].mon;
      }
    }
    if (now >= i) {
      minn = min(minn, sum);
    } else {
      int need = i - now;
      for (int j = 1, k = 1; j <= need; j++, k++) {
        if (k == m + 1) {
          sum = minn + 1;
          break;
        }
        if (all[k].used == true || all[k].to == 1) {
          j--;
          continue;
        }
        sum += all[k].mon;
      }
      minn = min(minn, sum);
    }
  }
  cout << minn << endl;
  return 0;
}
