#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 0, m = 0;
  scanf("%d%d", &n, &m);
  unsigned long long int res = 0, c = 0, bestRes = 1e16, resBuff = 0;
  vector<pair<int, unsigned long long int> > peoples(n);
  vector<int> k(m, 0);
  int id = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%I64d", &peoples[i].second, &peoples[i].first);
    k[peoples[i].second - 1]++;
    if (peoples[i].second == 1) {
      i--;
      n--;
      peoples.pop_back();
    }
  }
  vector<bool> use(n, false);
  if (peoples.size() > 1) {
    sort(peoples.begin(), peoples.end());
  }
  int maxValue = 0;
  for (int i = 1; i < m; i++) {
    if (maxValue < k[i]) {
      maxValue = k[i];
    }
  }
  vector<bool> used(n, false);
  for (int offset = 0;; offset++) {
    for (int i = 0; i < n; i++) {
      if (!used[i] && k[peoples[i].second - 1] > maxValue - offset) {
        used[i] = true;
        resBuff += peoples[i].first;
        k[peoples[i].second - 1]--;
        k[0]++;
      }
    }
    int kk = k[0];
    res = resBuff;
    for (int i = 0; i < n && kk <= maxValue - offset; i++) {
      if (!used[i]) {
        res += peoples[i].first;
        kk++;
      }
    }
    if (bestRes <= res) {
      printf("%I64d", bestRes);
      return 0;
    }
    bestRes = res;
  }
  return 0;
}
