#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > num(n);
  for (long long i = 0; i < n; i++) {
    cin >> num[i].second >> num[i].first;
    num[i].second--;
    num[i].first = -num[i].first;
  }
  long long ans = 1e18;
  for (long long x = 1; x <= n; x++) {
    long long res = 0;
    vector<pair<long long, long long> > all;
    vector<vector<pair<long long, long long> > > smth(m);
    for (long long i = 0; i < num.size(); i++)
      smth[num[i].second].push_back(num[i]);
    long long now = smth[0].size();
    for (long long i = 0; i < smth.size(); i++)
      sort(smth[i].begin(), smth[i].end());
    for (long long i = 1; i < smth.size(); i++) {
      if (smth[i].size() >= x) {
        long long k = smth[i].size() - x + 1;
        for (long long j = 0; j < k; j++) {
          res -= smth[i][smth[i].size() - 1].first;
          smth[i].pop_back();
          now++;
        }
      }
    }
    vector<pair<long long, long long> > also;
    for (long long i = 0; i < smth.size(); i++) {
      for (long long j = 0; j < smth[i].size(); j++) {
        also.push_back(smth[i][j]);
        if (smth[i][j].second == 0) also.pop_back();
      }
    }
    sort(also.begin(), also.end());
    while (now < x) {
      res -= also[also.size() - 1].first;
      also.pop_back();
      now++;
    }
    ans = min(ans, res);
  }
  cout << ans << "\n";
  return 0;
}
