#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> ppl(m);
  int pty, cost;
  for (int i = 0; i < n; i++) {
    cin >> pty >> cost;
    ppl[pty - 1].push_back(cost);
  }
  for (int i = 0; i < m; i++) {
    sort(ppl[i].begin(), ppl[i].end());
  }
  long long int ans = 1e15;
  for (int votes = 1; votes <= n; votes++) {
    long long int cost = 0;
    int my = ppl[0].size();
    vector<int> cheapest;
    for (int i = 1; i < m; i++) {
      for (int j = 0; j < ppl[i].size(); j++) {
        if (ppl[i].size() - j >= votes) {
          cost = cost + ppl[i][j];
          my++;
        } else {
          cheapest.push_back(ppl[i][j]);
        }
      }
    }
    sort(cheapest.begin(), cheapest.end());
    int ind = 0;
    while (my < votes) {
      cost = cost + cheapest[ind];
      ind++;
      my++;
    }
    ans = min(ans, cost);
  }
  cout << ans << "\n";
}
