#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adj(m);
  int first, second;
  for (int i = 0; i < n; i++) {
    cin >> first >> second;
    first--;
    adj[first].emplace_back(second);
  }
  for (vector<int>& v : adj) {
    sort((v).rbegin(), (v).rend());
  }
  long long ans = LLONG_MAX;
  for (int first = 1; first <= n; first++) {
    priority_queue<int, vector<int>> pq;
    long long cur = 0;
    long long votes = adj[0].size();
    for (int p = 1; p < m; p++) {
      for (int i = 0; i < first - 1 && i < adj[p].size(); i++)
        pq.push(-adj[p][i]);
      for (int i = first - 1; i < adj[p].size(); i++) {
        votes++;
        cur += adj[p][i];
      }
    }
    votes = first - votes;
    while (votes > 0) {
      votes--;
      cur -= pq.top();
      pq.pop();
    }
    ans = min(ans, cur);
  }
  cout << ans << endl;
}
