#include <bits/stdc++.h>
using namespace std;
const long long mxn = 3006;
long long n, m, p, c;
vector<long long> a[mxn];
long long minCost(long long x) {
  vector<long long> r;
  long long ans = 0;
  long long votes = a[0].size();
  for (long long i = 1; i <= m; i++) {
    long long j = 0, k = a[i].size();
    while (k >= x) {
      ans += a[i][j];
      j++;
      k--;
      votes++;
    }
    while (j < a[i].size()) {
      r.push_back(a[i][j]);
      j++;
    }
  }
  sort(r.begin(), r.end());
  for (long long i = 0; i < r.size(); i++) {
    if (votes >= x) {
      break;
    } else {
      votes++;
      ans += r[i];
    }
  }
  return ans;
}
int32_t main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> p >> c;
    if (p == 1) {
      a[1].push_back(0);
    } else {
      a[p].push_back(c);
    }
  }
  long long ans = 1e18;
  for (long long i = 1; i <= m; i++) {
    sort(a[i].begin(), a[i].end());
  }
  for (long long x = 1; x <= n; x++) {
    ans = min(ans, minCost(x));
  }
  cout << ans << "\n";
}
