#include <bits/stdc++.h>
using namespace std;
int stfill(int mx, multiset<pair<int, int> > &st, multiset<int> cost[], int m,
           int pc[]) {
  int ns = 0, x;
  for (int i = 1; i < m; i++) {
    if (pc[i] == mx) {
      x = *(cost[i].begin());
      st.insert(make_pair(x, i));
      ns++;
    }
  }
  return ns;
}
long long int findcost(multiset<pair<int, int> > &s, int ns) {
  int cnt = ns;
  long long int cc = 0;
  multiset<pair<int, int> >::iterator it = s.begin();
  while (cnt--) {
    cc += it->first;
    it++;
  }
  return cc;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, m, p, c, i, v, mx = 0;
  long long int cc = 0;
  cin >> n >> m;
  int pc[m];
  for (i = 0; i < m; i++) pc[i] = 0;
  multiset<int> cost[m];
  multiset<int>::iterator it;
  multiset<pair<int, int> > s;
  multiset<pair<int, int> >::iterator sit, sitt;
  multiset<pair<int, int> > st;
  for (i = 0; i < n; i++) {
    cin >> p >> c;
    pc[p - 1]++;
    cost[p - 1].insert(c);
    if (p > 1) s.insert(make_pair(c, p - 1));
  }
  for (i = 1; i < m; i++) {
    mx = max(pc[i], mx);
  }
  v = mx + 1 - pc[0];
  long long int va, vb;
  int ns, t;
  ns = stfill(mx, st, cost, m, pc);
  while (v > 0) {
    if (v > ns) {
      va = findcost(s, ns + 1);
      vb = findcost(st, ns);
      if (va < vb) {
        sit = s.begin();
        pc[sit->second]--;
        cost[sit->second].erase(sit->first);
        cc += sit->first;
        s.erase(sit);
        v--;
      } else {
        mx--;
        cc += vb;
        t = ns;
        sit = st.begin();
        while (t--) {
          pc[sit->second]--;
          cost[sit->second].erase(sit->first);
          s.erase(make_pair(sit->first, sit->second));
          sitt = sit;
          st.erase(sitt);
          sit++;
        }
        v -= (ns + 1);
      }
      st.clear();
      ns = stfill(mx, st, cost, m, pc);
    } else {
      va = findcost(s, v);
      cc += va;
      v = 0;
    }
  }
  cout << cc;
  return 0;
}
