#include <bits/stdc++.h>
using namespace std;
vector<int> v[3005];
int cnt[3005];
vector<int> q;
int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    int p = read(), c = read();
    v[p].push_back(c);
    ++cnt[p];
  }
  for (int i = 1; i <= m; i++) sort(v[i].begin(), v[i].end());
  long long ans = 1LL << 60;
  for (int i = 1; i <= n; i++) {
    long long s = 0;
    int p = cnt[1];
    q.clear();
    for (int j = 2; j <= m; j++) {
      if (cnt[j] >= i) {
        for (int k = 0; k <= cnt[j] - i; k++) {
          s += v[j][k];
          ++p;
        }
        for (int k = cnt[j] - i + 1; k < v[j].size(); k++) q.push_back(v[j][k]);
      } else
        for (int k = 0; k < v[j].size(); k++) q.push_back(v[j][k]);
    }
    if (p < i) {
      sort(q.begin(), q.end());
      for (int j = p; j < i; j++) s += q[j - p];
    }
    ans = min(ans, s);
  }
  printf("%I64d\n", ans);
  return 0;
}
