#include <bits/stdc++.h>
using namespace std;
long long n, m, counter, x, y;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
priority_queue<int, vector<int>, greater<int> > q[3005];
int ap[3005];
void update() {
  while (!pq.empty()) {
    if (q[pq.top().second].top() > pq.top().first)
      pq.pop();
    else
      break;
  }
}
void remove_top() {
  counter += pq.top().first;
  q[pq.top().second].pop();
  ap[pq.top().second]--;
  pq.pop();
  ap[1]++;
  update();
}
void compare(int howmany, int max_ap, long long sum) {
  update();
  vector<pair<int, int> > del;
  long long aux = 0;
  while (howmany > del.size()) {
    del.push_back(pq.top());
    aux += pq.top().first;
    pq.pop();
    update();
  }
  for (int i = 0; i < howmany; i++) pq.push(del[i]);
  if (aux < sum)
    remove_top();
  else {
    for (int i = 2; i <= m; i++)
      if (ap[i] == max_ap) {
        counter += q[i].top();
        ap[1]++;
        ap[i]--;
        q[i].pop();
      }
    update();
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    ap[x]++;
    if (x == 1) continue;
    q[x].push(y);
    pq.push({y, x});
  }
  for (int i = 1; i <= n; i++) {
    int max_ap = 0;
    int howmany;
    int ind;
    long long sum = 0;
    for (int j = 1; j <= m; j++) {
      if (ap[j] > max_ap) {
        max_ap = ap[j];
        howmany = 1;
        ind = j;
        if (j != 1) sum = q[j].top();
        continue;
      }
      if (ap[j] == max_ap) {
        if (!q[j].empty()) sum += q[j].top();
        howmany++;
      }
    }
    if (ind == 1 && howmany == 1) {
      cout << counter;
      return 0;
    }
    if (howmany > max_ap - ap[1] || pq.size() == 1) {
      remove_top();
      continue;
    }
    compare(howmany + 1, max_ap, sum);
  }
  cout << counter;
  return 0;
}
