#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> pri[3005];
long long ans;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int par, mon;
    cin >> par >> mon;
    pri[par].push_back(mon);
  }
  for (int i = 1; i <= m; i++) sort(pri[i].begin(), pri[i].end());
  ans = 1e18 + 5;
  for (int i = 1; i <= n; i++) {
    long long cost = 0;
    int need = i - pri[1].size();
    vector<int> U;
    for (int j = 2; j <= m; j++) {
      int staIn = max(0, (int)pri[j].size() - (i - 1));
      for (int k = 0; k < staIn; k++) {
        cost += pri[j][k];
        need--;
      }
      for (int k = staIn; k < pri[j].size(); k++) {
        U.push_back(pri[j][k]);
      }
    }
    sort(U.begin(), U.end());
    if (need > 0) {
      for (int j = 0; j < need; j++) cost += U[j];
    }
    ans = min(cost, ans);
  }
  cout << ans << endl;
  return 0;
}
