#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int p[4000], c[4000];
  int cnt[4000] = {0};
  vector<int> belong[4000];
  int pos[4000] = {0};
  for (int i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    cnt[p[i]]++;
    belong[p[i]].push_back(c[i]);
  }
  for (int i = 1; i <= m; i++) sort(belong[i].begin(), belong[i].end());
  long long best_price = 1000000000000000;
  for (int gres = std::max(cnt[1], 1); gres <= n; gres++) {
    int cur_count = cnt[1];
    long long cur_price = 0;
    for (int j = 2; j <= m; j++)
      if (cnt[j] >= gres) {
        int take = cnt[j] - gres + 1;
        cur_count += take;
        if (cur_count > gres) break;
        for (int k = 0; k < take; k++) cur_price += belong[j][k];
        pos[j] = take;
      } else
        pos[j] = 0;
    if (cur_count > gres || cur_price >= best_price) continue;
    if (cur_count < gres) {
      int take = gres - cur_count;
      priority_queue<int> qq;
      for (int j = 2; j <= m; j++)
        for (int k = pos[j]; k < cnt[j]; k++) {
          if (qq.size() < take || belong[j][k] < qq.top()) {
            if (qq.size() == take) qq.pop();
            qq.push(belong[j][k]);
          }
        }
      while (!qq.empty()) {
        cur_price += qq.top();
        qq.pop();
      }
    }
    best_price = std::min(best_price, cur_price);
  }
  cout << best_price;
}
