#include <bits/stdc++.h>
using namespace std;
int GLL(long long& x) { return scanf("%lld", &x); }
int GI(int& x) { return scanf("%d", &x); }
vector<vector<int>> bribes;
int n, m, p, c;
int main() {
  GI(n);
  GI(m);
  bribes.resize(m + 1);
  for (int i = 1; i <= n; i++) {
    GI(p);
    GI(c);
    bribes[p].push_back(c);
  }
  for (int i = 1; i <= m; i++) {
    sort(bribes[i].rbegin(), bribes[i].rend());
  }
  long long best = 1LL * 2000000000 * 2000000000;
  for (int i = 1; i <= n; i++) {
    int votes = bribes[1].size();
    long long cost = 0;
    vector<int> cheapest;
    for (int j = 2; j <= m; j++) {
      for (int k = 0; k < bribes[j].size(); k++) {
        if (k >= i - 1) {
          cost += bribes[j][k];
          votes++;
        } else {
          cheapest.push_back(bribes[j][k]);
        }
      }
    }
    sort(cheapest.rbegin(), cheapest.rend());
    while (!cheapest.empty() && votes < i) {
      cost += cheapest.back();
      cheapest.pop_back();
      votes++;
    }
    if (votes >= i) {
      best = min(best, cost);
    }
  }
  printf("%lld\n", best);
  return 0;
}
