#include <bits/stdc++.h>
using namespace std;
const int N = 3000 + 1;
int n, m;
vector<int> pay[N];
vector<int> remain;
long long res;
int main() {
  ios_base::sync_with_stdio(false);
  srand(time(NULL));
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int c, p;
    cin >> p >> c;
    pay[p].emplace_back(c);
    res += c;
  }
  for (int i = 1; i <= m; i++) sort(pay[i].begin(), pay[i].end());
  for (int bound = 1; bound <= n; bound++) {
    long long cost = 0;
    int vote = int(pay[1].size());
    remain.clear();
    for (int i = 2; i <= m; i++)
      if (int(pay[i].size()) >= bound) {
        int l = int(pay[i].size()) - bound;
        for (int j = 0; j <= l; j++) {
          vote++;
          cost += pay[i][j];
        }
        for (int j = l + 1; j <= int(pay[i].size()) - 1; j++)
          remain.emplace_back(pay[i][j]);
      } else {
        for (int j = 0; j < int(pay[i].size()); j++)
          remain.emplace_back(pay[i][j]);
      }
    if (vote >= bound)
      res = min(res, cost);
    else {
      sort(remain.begin(), remain.end());
      for (int j = 0; j <= bound - vote - 1; j++) cost += remain[j];
      res = min(res, cost);
    }
  }
  cout << res;
}
