#include <bits/stdc++.h>
using namespace std;
struct node {
  int p, w;
  long long c;
  bool operator<(const node &T) const { return c > T.c; }
} a[3005];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  bool vis[3005];
  int n, m, num[3005], sum;
  priority_queue<node> que[3005], t;
  long long ans = 0x3f3f3f3f3f3f3f3f, tans;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    if (!que[i].empty()) que[i].pop();
  memset(num, 0, sizeof num);
  for (int i = 1; i <= n; i++) {
    cin >> a[i].p >> a[i].c;
    num[a[i].p]++;
    a[i].w = i;
    que[a[i].p].push(a[i]);
  }
  sort(a + 1, a + n + 1, [&](node a, node b) { return a.c < b.c; });
  for (int i = 1; i <= (n + 1) / 2; i++) {
    sum = num[1];
    memset(vis, 0, sizeof vis);
    tans = 0;
    for (int j = 2; j <= m; j++) {
      t = que[j];
      for (int k = i; k <= num[j]; k++) {
        tans += t.top().c;
        vis[t.top().w] = 1;
        t.pop();
        sum++;
      }
    }
    for (int j = sum + 1, k = 1; j <= i; k++) {
      if (!vis[a[k].w] && a[k].p != 1) {
        tans += a[k].c;
        j++;
      }
    }
    ans = min(ans, tans);
  }
  cout << ans << endl;
  return 0;
}
