#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long INF = 0x3f3f3f3f3f3f3f;
long long p[maxn], c[maxn];
vector<long long> cnt[maxn];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    cnt[p[i]].push_back(c[i]);
  }
  for (int i = 1; i <= m; i++) sort(cnt[i].begin(), cnt[i].end());
  long long ans = INF;
  vector<long long> val;
  for (int x = (int)cnt[1].size(); x <= n; x++) {
    long long sum = 0;
    int f = 0;
    val.clear();
    int need = x - cnt[1].size();
    for (int i = 2; i <= m; i++) {
      int k = 0;
      if (cnt[i].size() >= x) k = (int)cnt[i].size() - x + 1;
      if (k > need) {
        f = 1;
        break;
      } else {
        need -= k;
        for (int j = 0; j < k; j++) sum += 1LL * cnt[i][j];
        for (int j = k; j < (int)cnt[i].size(); j++) val.push_back(cnt[i][j]);
      }
    }
    if (f) continue;
    sort(val.begin(), val.end());
    for (int i = 0; i < need; i++) {
      sum += val[i];
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
