#include <bits/stdc++.h>
using namespace std;
long long int n, m;
std::vector<long long int> pcc[3001];
std::vector<long long int> extravotes;
long long int markers[3001] = {0};
long long int checkwin(long long int k) {
  long long int myvotes = pcc[1].size();
  long long int cost = 0;
  for (int i = 2; i <= m; ++i) {
    int arrsize = pcc[i].size();
    if (arrsize == 0) continue;
    for (int j = 0; j < arrsize - k + 1; j++) {
      cost += pcc[i][j];
      myvotes++;
      markers[i] = j + 1;
    }
  }
  int cpmyvotes = myvotes;
  if (myvotes >= k)
    return cost;
  else {
    for (int i = 2; i <= m; ++i) {
      int arrsize = pcc[i].size();
      if (arrsize == 0) continue;
      for (int j = markers[i]; j < arrsize; ++j)
        extravotes.push_back(pcc[i][j]);
    }
    sort(extravotes.begin(), extravotes.end());
    for (int i = 0; i < k - cpmyvotes && i < extravotes.size(); ++i) {
      cost += extravotes[i];
      myvotes++;
    }
    if (myvotes >= k) return cost;
  }
  return 1e18;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    long long int p, c;
    cin >> p >> c;
    pcc[p].push_back(c);
  }
  for (int i = 1; i <= m; ++i) sort(pcc[i].begin(), pcc[i].end());
  long long int ans = 1e18 + 3;
  int flag = 0;
  for (int i = 2; i <= m; ++i) {
    if (pcc[i].size() >= pcc[1].size()) flag = 1;
  }
  if (!flag) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    long long int mini = checkwin(i);
    ans = min(mini, ans);
    extravotes.clear();
    for (int w = 0; w <= m; ++w) markers[w] = 0;
  }
  cout << ans << endl;
  return 0;
}
