#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
vector<int> occ[N];
vector<long long> pref[N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int p, x;
    scanf("%d %d", &p, &x);
    occ[p].push_back(x);
  }
  for (int i = 1; i <= m; i++) {
    sort(occ[i].begin(), occ[i].end());
    for (int j = 0; j < (int)occ[i].size(); j++) {
      long long val = occ[i][j];
      if (j) val += pref[i][j - 1];
      pref[i].push_back(val);
    }
  }
  int udah = occ[1].size();
  long long ans = 1e15;
  for (int ada = max(udah, 1); ada <= n; ada += 1) {
    long long cur = 0;
    int dapat = udah;
    priority_queue<pair<int, pair<int, int> >,
                   vector<pair<int, pair<int, int> > >,
                   greater<pair<int, pair<int, int> > > >
        pq;
    for (int i = 2; i <= m; i++) {
      int kurang = 0;
      if ((int)occ[i].size() >= ada) {
        kurang = (int)occ[i].size() - ada + 1;
        dapat += kurang;
        cur += pref[i][kurang - 1];
      }
      int sz = (int)occ[i].size();
      if (kurang < sz) {
        pq.push(make_pair(occ[i][kurang], make_pair(i, kurang + 1)));
      }
    }
    if (dapat < ada) {
      long long overhead = 0;
      while (!pq.empty()) {
        pair<int, pair<int, int> > top = pq.top();
        pq.pop();
        cur += top.first;
        dapat += 1;
        if (dapat == ada) break;
        int sz = occ[top.second.first].size();
        if (top.second.second < sz) {
          pq.push(
              make_pair(occ[top.second.first][top.second.second],
                        make_pair(top.second.first, top.second.second + 1)));
        }
      }
    }
    ans = min(ans, cur);
  }
  printf("%lld\n", ans);
  return 0;
}
