#include <bits/stdc++.h>
using namespace std;
int dcmp(double a, double b) {
  return ((fabs(a - b) < (1e-9)) ? 0 : (a < b) ? -1 : +1);
}
double crossP(const complex<double> &v1, const complex<double> &v2) {
  return (conj(v1) * v2).imag();
}
double dotP(const complex<double> &v1, const complex<double> &v2) {
  return (conj(v1) * v2).real();
}
double lengthSqr(const complex<double> &p) { return dotP(p, p); }
double length(const complex<double> &p) { return hypot(p.real(), p.imag()); }
double fixAngle(double a) { return ((a > 1) ? 1 : (a < -1) ? -1 : a); }
bool same(const complex<double> &p1, const complex<double> &p2) {
  return ((dcmp(p1.real(), p2.real()) == 0 && dcmp(p1.imag(), p2.imag()) == 0)
              ? 1
              : 0);
}
double getAngle_A_abc(double a, double b, double c) {
  return acos(fixAngle((b * b + c * c - a * a) / (2 * b * c)));
}
const int N = 3000 + 9;
map<int, vector<int> > v;
multiset<int> q;
long long calc(int mx, int n, int d) {
  q.clear();
  long long cost = 0;
  for (const auto &a : v) {
    const vector<int> &x = a.second;
    int s = ((int)((x).size()));
    int i = 0;
    while (s > mx) d++, cost += x[i], s--, i++;
    while (i < ((int)((x).size())) && d <= mx) q.emplace(x[i++]);
  }
  auto it = q.begin();
  while (d <= mx) d++, cost += *it++;
  return cost;
}
int main() {
  int t, n;
  scanf("%d %d", &t, &n);
  long long d = 0;
  for (__typeof(t) i = 0; i < t; ++i) {
    int p, c;
    scanf("%d %d", &p, &c);
    --p;
    if (p)
      v[p].emplace_back(c);
    else
      d++;
  }
  int max_v = 0;
  for (auto &a : v)
    max_v = max(max_v, ((int)((a.second).size()))),
    sort(a.second.begin(), a.second.end());
  long long ans = (~(1LL << 63));
  for (__typeof(max_v + 1) i = 0; i < max_v + 1; ++i)
    ans = min(ans, calc(i, n, d));
  printf("%I64d", ans);
  return 0;
}
