#include <bits/stdc++.h>
using namespace std;
int II() {
  int q;
  scanf("%d", &q);
  return q;
}
long long LII() {
  long long q;
  scanf("%lld", &q);
  return q;
}
long long N, M;
vector<pair<long long, long long>> v;
long long mot, cnt;
map<long long, vector<long long>> mp;
long long calc_1(long long mid) {
  long long cost = 0;
  vector<long long> vv;
  map<long long, long long> mp1;
  long long cc = 0;
  for (auto it : mp) {
    for (long long i = 0; i < it.second.size(); i++) {
      if (i < (mid - 1))
        vv.push_back(it.second[i]), mp1[it.first]++;
      else
        cost += it.second[i], cc++;
    }
  }
  sort(vv.begin(), vv.end());
  reverse(vv.begin(), vv.end());
  while ((cc + cnt) < mid && vv.size() > 0) {
    cost += vv.back();
    vv.pop_back();
    cc++;
  }
  return cost;
}
void solve() {
  cin >> N >> M;
  for (long long i = 1; i <= N; i++) {
    long long l, r;
    cin >> l >> r;
    if (l == 1)
      mot += r, cnt++;
    else
      v.push_back({l, r});
  }
  for (auto it : v) {
    mp[it.first].push_back(it.second);
  }
  long long l = cnt, r = cnt + v.size();
  bool bo = 1;
  for (auto it : mp) {
    if (it.second.size() >= cnt) bo = 0;
    vector<long long> temp = it.second;
    sort(temp.begin(), temp.end(),
         [](long long a, long long b) { return a > b; });
    mp[it.first] = temp;
  }
  if (bo) {
    cout << 0 << endl;
    return;
  }
  long long mi = 1e18;
  for (long long i = cnt; i < N; i++) mi = min(mi, calc_1(i));
  cout << mi << endl;
}
int32_t main() {
  long long tt;
  tt = 1;
  while (tt--) {
    solve();
  }
}
