#include <bits/stdc++.h>
using namespace std;
const int N = 3003;
int p[N];
long long c[N];
int n, m;
vector<long long> guy[N];
vector<long long> sguy[N];
long long f(int b) {
  long long ans = 0;
  int cnt = (int)guy[1].size();
  for (int i = 2; i <= m; i++)
    if ((int)guy[i].size() >= b) {
      int t = (int)guy[i].size() - b + 1;
      cnt += t;
      ans += sguy[i][t - 1];
    }
  priority_queue<long long> q;
  for (int i = 2; i <= m; i++) {
    if ((int)guy[i].size() >= b) {
      for (int j = (int)guy[i].size() - b + 1; j < (int)guy[i].size(); j++) {
        q.push(guy[i][j]);
        if ((int)q.size() > b - cnt) q.pop();
      }
    } else {
      for (long long x : guy[i]) {
        q.push(x);
        if ((int)q.size() > b - cnt) q.pop();
      }
    }
  }
  while (!q.empty()) ans += q.top(), q.pop();
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d %lld", p + i, c + i);
  if (n == 1) {
    if (p[0] == 1) return printf("0\n"), 0;
    return printf("%lld\n", c[0]), 0;
  }
  if (n == 2) {
    long long ans = 0;
    if (p[0] != 1) ans += c[0];
    if (p[1] != 1) ans += c[1];
    return printf("%lld\n", ans), 0;
  }
  for (int i = 0; i < n; i++) guy[p[i]].push_back(c[i]);
  for (int i = 1; i <= m; i++) {
    sort(guy[i].begin(), guy[i].end());
    sguy[i].resize(guy[i].size());
    partial_sum(guy[i].begin(), guy[i].end(), sguy[i].begin());
  }
  long long ans = 1e18;
  for (int i = 1; i <= n; i++) ans = min(ans, f(i));
  printf("%lld\n", ans);
}
