#include <bits/stdc++.h>
using namespace std;
const int nmax = 3000;
bool viz[nmax + 1];
vector<pair<int, int>> toti;
vector<pair<int, int>> v[nmax + 1];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int p, c;
    cin >> p >> c;
    v[p].push_back({c, i});
    if (p != 1) toti.push_back({c, i});
  }
  sort(toti.begin(), toti.end());
  for (int i = 1; i <= m; ++i) {
    sort(v[i].begin(), v[i].end());
  }
  long long ans = 1LL << 60;
  for (int i = 0; i <= n - (int)v[1].size(); ++i) {
    int lim = v[1].size() + i;
    if (lim == 0) continue;
    long long sum = 0;
    int pusi = 0;
    vector<int> cn;
    for (int j = 2; j <= m; ++j) {
      for (int k = 0; k <= (int)v[j].size() - lim; ++k) {
        sum += v[j][k].first;
        cn.push_back(v[j][k].second);
        viz[v[j][k].second] = 1;
        ++pusi;
      }
    }
    for (int j = 0; j < (int)toti.size() && pusi < i; ++j) {
      if (viz[toti[j].second] == 0) {
        viz[toti[j].second] = 1;
        cn.push_back(toti[j].second);
        ++pusi;
        sum += toti[j].first;
      }
    }
    for (auto j : cn) viz[j] = 0;
    if (pusi >= i) {
      ans = min(ans, sum);
    }
  }
  cout << ans << "\n";
  return 0;
}
