#include <bits/stdc++.h>
using namespace std;
int n, m;
long long cost;
vector<int> G[3002];
void solve(int q) {
  long long act = 0;
  int muiepsd = G[1].size();
  for (int i = 2; i <= m; ++i)
    if ((int)G[i].size() >= q)
      for (int j = 0; j < (int)G[i].size() - q + 1; ++j)
        muiepsd++, act += G[i][j];
  if (act > cost) return;
  if (muiepsd >= q) {
    cost = min(cost, act);
    return;
  }
  vector<int> V;
  for (int i = 2; i <= m; ++i)
    if ((int)G[i].size() >= q)
      for (int j = G[i].size() - q + 1; j < (int)G[i].size(); ++j)
        V.push_back(G[i][j]);
    else
      for (int j = 0; j < (int)G[i].size(); ++j) V.push_back(G[i][j]);
  sort(V.begin(), V.end());
  for (int i = 0; i < (int)V.size(); ++i) {
    act += V[i];
    if (++muiepsd == q) break;
  }
  cost = min(cost, act);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
  }
  for (int i = 1; i <= m; ++i)
    if ((int)G[i].size()) sort(G[i].begin(), G[i].end());
  cost = 1LL << 60;
  for (int i = max(1, (int)G[1].size()); i <= n; ++i) solve(i);
  cout << cost;
  return 0;
}
