#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e15;
const int M = 1e9 + 7;
std::vector<int> party[5000000];
int votes;
int m, n, a, b;
vector<pair<long long int, long long int> > cost;
long long int spend = inf;
priority_queue<long long int> pq;
long long int tryfor(long long int k) {
  long long int i, j, elem = 0, costss = 0, sz, u;
  elem = votes;
  for (i = 1; i < m; ++i) {
    sz = party[i].size();
    for (j = 0; j < sz - k + 1; ++j) {
      costss += party[i][j];
      elem++;
    }
    for (j = max(0LL, sz - k + 1LL); j < sz; ++j) pq.push(-1 * party[i][j]);
  }
  while (elem < k) {
    u = pq.top();
    costss -= u;
    elem++;
    pq.pop();
  }
  while (!(pq.empty())) pq.pop();
  return costss;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    if (--a != 0)
      cost.emplace_back(b, a);
    else
      votes++;
    party[a].push_back(b);
  }
  sort(cost.begin(), cost.end());
  for (int i = 0; i < m; ++i) sort(party[i].begin(), party[i].end());
  for (int i = max(votes, 1); i <= n; ++i) {
    spend = min(spend, tryfor(i));
  }
  cout << spend << endl;
  return 0;
}
