#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, long long>> v;
vector<multiset<long long>> cnt;
multimap<int, multiset<long long>> mm;
multimap<int, multiset<long long>>::iterator it;
long long minn = 9000000000000000000, minnvoter = minn;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  v.resize(n);
  cnt.resize(m + 1);
  for (int i = 0; i < n; i++)
    cin >> v[i].first >> v[i].second, cnt[v[i].first].insert(v[i].second),
        minnvoter = min(minnvoter, v[i].second);
  for (int i = 2; i <= m; i++)
    if (cnt[i].size() > 0)
      mm.insert(pair<int, multiset<long long>>(cnt[i].size(), cnt[i]));
  if (m == 1) {
    cout << 0;
    return 0;
  }
  if (mm.empty()) {
    if (cnt[1].size() == 0)
      cout << minnvoter;
    else
      cout << 0;
    return 0;
  }
  it = mm.end();
  it--;
  if (cnt[1].size() > it->first) {
    cout << 0;
    return 0;
  }
  for (int k = 1; k <= n; k++) {
    multiset<long long> ms;
    long long res = 0;
    int d = cnt[1].size();
    it = mm.begin();
    for (int i = 0; i < mm.size(); i++) {
      if (it->first >= k) {
        multiset<long long>::iterator its;
        its = it->second.begin();
        for (int j = 0; j < it->second.size(); j++)
          if (j < it->first - k + 1) {
            res += *its;
            d += 1;
            its++;
          } else
            ms.insert(*its), its++;
      } else {
        multiset<long long>::iterator its;
        for (its = it->second.begin(); its != it->second.end(); its++)
          ms.insert(*its);
      }
      it++;
    }
    multiset<long long>::iterator its;
    its = ms.begin();
    while (its != ms.end() && d < k) {
      d += 1;
      res += *its;
      its++;
    }
    if (d >= k) minn = min(minn, res);
  }
  cout << minn;
  return 0;
}
