#include <bits/stdc++.h>
using namespace std;
const int MAXM = 3e3 + 10;
const long long int INF = 1e18;
vector<int> ps[MAXM];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  multiset<int> s;
  for (int i = 0; i < int(n); i++) {
    int p, c;
    cin >> p >> c;
    p--;
    ps[p].emplace_back(c);
    if (p != 0) s.insert(c);
  }
  for (int i = 0; i < int(m); i++) sort(ps[i].begin(), ps[i].end());
  long long int ans = INF;
  int best, mx = 0;
  for (int i = 0; i < int(m); i++)
    if (int((ps[i]).size()) >= mx) {
      best = i;
      mx = int((ps[i]).size());
    }
  if (best == 0)
    ans = 0;
  else {
    for (int target = (int)(1); target < (int)(n + 1); target++) {
      multiset<int> act = s;
      long long int cost = 0;
      int h = int((ps[0]).size());
      for (int i = (int)(1); i < (int)(m); i++) {
        if (int((ps[i]).size()) >= target) {
          for (int j = 0; j < int(int((ps[i]).size()) - target + 1); j++) {
            h++;
            cost += ps[i][j];
            act.erase(act.find(ps[i][j]));
          }
        }
      }
      while (h < target) {
        cost += *act.begin();
        act.erase(act.begin());
        h++;
      }
      ans = min(ans, cost);
    }
  }
  cout << ans << '\n';
  return 0;
}
