#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 20, K = 17;
const int mod = 1e9 + 9;
vector<int> election[3011];
int it[N];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int e, val;
    scanf("%d%d", &e, &val);
    election[e - 1].push_back(val);
  }
  int mx = 0;
  for (int i = 0; i < m; i++)
    if (election[i].size()) {
      mx = max(mx, (int)election[i].size());
      sort(election[i].begin(), election[i].end());
    }
  long long ans = 2e18;
  for (int i = 0; i < n; i++) {
    long long cost = 0;
    int vote = election[0].size();
    for (int j = 1; j < m; j++) {
      it[j] = 0;
      while (election[j].size() - it[j] > i) {
        cost += election[j][it[j]++];
      }
      vote += it[j];
    }
    priority_queue<int> pq;
    for (int j = 1; j < m; j++)
      for (int k = it[j]; k < election[j].size(); k++) pq.push(-election[j][k]);
    assert(vote + pq.size() >= i + 1);
    while (vote < i + 1) {
      cost += -pq.top();
      pq.pop();
      vote++;
    }
    ans = min(ans, cost);
  }
  cout << ans;
  return 0;
}
