#include <bits/stdc++.h>
using std::priority_queue;
struct node {
  int n;
  long long ans;
  node(int n, long long ans) {
    this->n = n;
    this->ans = ans;
  }
  friend bool operator<(node a, node b) { return a.ans > b.ans; }
};
int money[3011][3010];
int cnt[3010];
int main() {
  int n, m, u, v;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &u, &v);
    money[u][++money[u][0]] = v;
  }
  for (int i = 1; i <= m; i++)
    std::sort(money[i] + 1, money[i] + 1 + money[i][0]);
  long long ans = 1000000000000001ll;
  for (int mid = 1; mid <= (n + 1 >> 1); mid++) {
    priority_queue<node> q;
    long long sum = 0, now = money[1][0];
    for (int i = 2; i <= m; i++) {
      for (int j = 1; j <= money[i][0] - mid + 1; j++) {
        sum += money[i][j];
        ++now;
      }
      cnt[i] = std::max(0, money[i][0] - mid + 1);
      if (cnt[i] < money[i][0]) {
        cnt[i]++;
        q.push(node(i, money[i][cnt[i]]));
      }
    }
    for (int i = now + 1; i <= mid; i++) {
      if (q.empty()) break;
      node k = q.top();
      q.pop();
      sum += k.ans;
      if (cnt[k.n] < money[k.n][0]) q.push(node(k.n, money[k.n][++cnt[k.n]]));
    }
    ans = ans < sum ? ans : sum;
  }
  printf("%I64d\n", ans);
  return 0;
}
