#include <bits/stdc++.h>
using namespace std;
long long votes[5000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long coin[n], party[n];
  multiset<long long> sm;
  vector<long long> voters[m + 1];
  vector<long long> cns[m + 1];
  for (long long i = 0; i < n; i++) {
    cin >> party[i] >> coin[i];
    votes[party[i]]++;
    voters[party[i]].push_back(i);
    cns[party[i]].push_back(coin[i]);
  }
  for (long long i = 2; i <= m; i++) sort(cns[i].begin(), cns[i].end());
  vector<long long> pref[m + 1];
  for (long long i = 2; i <= m; i++) {
    for (auto j : cns[i]) {
      if (pref[i].empty())
        pref[i].push_back(j);
      else
        pref[i].push_back(pref[i].back() + j);
    }
  }
  long long ans = 1e18;
  for (long long i = 2; i <= m; i++) {
    if (votes[i] <= votes[1])
      for (auto j : voters[i]) sm.insert(coin[j]);
  }
  vector<long long> v1;
  for (auto j : sm) {
    if (v1.empty())
      v1.push_back(j);
    else
      v1.push_back(v1.back() + j);
  }
  for (long long i = 0; i <= n - votes[1]; i++) {
    long long cur = 0;
    long long done = 0;
    if (i == 0 && votes[1] == 0) continue;
    bool poss = true;
    multiset<long long> mt;
    for (long long i = 0; i < n; i++)
      if (party[i] != 1) mt.insert(coin[i]);
    for (long long j = 2; j <= m; j++) {
      long long diff = (votes[j] - votes[1] - i) + 1;
      if (diff > 0) {
        if (pref[j].size() <= diff - 1) {
          poss = false;
          break;
        }
        done += diff;
        long long c = 0;
        while (diff--) {
          cur += cns[j][c];
          mt.erase(mt.find(cns[j][c++]));
        }
      }
    }
    if (mt.size() <= i - done - 1) poss = false;
    if (!poss) continue;
    long long c1 = 0;
    for (auto j : mt) {
      if (c1 == i - done) break;
      c1++;
      cur += j;
    }
    ans = min(ans, cur);
  }
  cout << ans << "\n";
}
