#include <bits/stdc++.h>
using namespace std;
long long n, m, ci, pi, add, rem, mini, ans, tmp, val, cnt[3050], tmpcnt[3050];
vector<pair<long long, long long> > vtrs, tmpvtrs;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> pi >> ci;
    if (pi == 1)
      val++;
    else {
      vtrs.push_back(make_pair(ci, pi));
      cnt[pi]++;
    }
  }
  n -= val;
  sort(vtrs.begin(), vtrs.end());
  for (int i = 0; i < vtrs.size(); i++) tmpvtrs.push_back(vtrs[i]);
  mini = 1000000000000;
  for (long long k = 0; k <= n; k++) {
    bool flag = 1;
    tmp = 0, add = k;
    for (int i = 1; i <= m; i++) tmpcnt[i] = cnt[i];
    for (int i = 0; i < vtrs.size(); i++) tmpvtrs[i] = vtrs[i];
    for (int i = 0; i < vtrs.size(); i++)
      if (tmpcnt[tmpvtrs[i].second] >= k + val) {
        if (add < 0) break;
        tmp += tmpvtrs[i].first;
        tmpcnt[tmpvtrs[i].second]--;
        tmpvtrs[i].first = -1;
        add--;
      }
    if (add < 0) flag = 0;
    if (flag) {
      for (int i = 0; i < vtrs.size(); i++) {
        if (add == 0) break;
        if (tmpvtrs[i].first != -1) tmp += tmpvtrs[i].first, add--;
      }
      mini = min(mini, tmp);
    }
  }
  cout << mini;
  return 0;
}
