#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  long long int a[n], b[n];
  vector<long long int> *partystats = new vector<long long int>[m];
  for (long long int i = 0; i < n; i++) {
    scanf("%lld %lld", &a[i], &b[i]);
    partystats[a[i] - 1].push_back(b[i]);
  }
  for (long long int i = 1; i < m; i++)
    sort(partystats[i].begin(), partystats[i].end());
  long long int cMin = ((long long int)1e18);
  for (long long int i = 1; i <= n; i++) {
    long long int c = 0L;
    vector<long long int> rem;
    long long int needed = i - partystats[0].size();
    for (long long int j = 1; j < m; j++) {
      int si = max((int)(partystats[j].size() - (i - 1)), 0);
      for (int k = 0; k < si; k++) {
        c += partystats[j][k];
        needed--;
      }
      for (long long int k = si; k < partystats[j].size(); k++) {
        rem.push_back(partystats[j][k]);
      }
    }
    if (needed > 0L) {
      nth_element(rem.begin(), rem.begin() + needed, rem.end());
      c += accumulate(rem.begin(), rem.begin() + needed, 0LL);
    }
    cMin = min(c, cMin);
  }
  printf("%lld", cMin);
  return 0;
}
