#include <bits/stdc++.h>
using namespace std;
int arr[5000];
long long int cost[5000];
vector<long long int> v[5000];
bool cmp(long long int a, long long int b) {
  if (a > b) return true;
  return false;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> arr[i] >> cost[i];
  for (int i = 0; i < n; i++) {
    v[arr[i] - 1].push_back(cost[i]);
  }
  long long int ans = 10000000000000000;
  for (int i = 0; i < m; i++) sort(v[i].begin(), v[i].end(), cmp);
  for (int M = 0; M < n; M++) {
    int votes = v[0].size();
    long long int can = 0;
    priority_queue<long long int> pq;
    for (int i = 1; i < m; i++) {
      for (int j = 0; j < v[i].size(); j++) {
        if (j >= M) {
          can += v[i][j];
          votes++;
        } else {
          pq.push(-v[i][j]);
        }
      }
    }
    while (!pq.empty() && votes <= M) {
      can -= pq.top();
      pq.pop();
      votes++;
    }
    ans = min(ans, can);
  }
  cout << ans << endl;
  return 0;
}
