#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3123;
pair<long long, long long> v[MAXN];
int notmine[MAXN];
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (n); ++i) {
    cin >> v[i].second >> v[i].first;
  }
  sort(v, v + n, greater<pair<long long, long long>>());
  long long ans = 0x3f3f3f3f3f3f3f3fll;
  for (int goal = 1; goal < (n + 1); ++goal) {
    int sz = 0;
    int myvotes = 0;
    long long pans = 0;
    unordered_map<int, int> mp;
    for (int i = 0; i < (n); ++i) {
      int c = v[i].first;
      int p = v[i].second;
      if (p == 1) {
        ++myvotes;
        continue;
      }
      if (mp[p] + 1 < goal) {
        ++mp[p];
        notmine[sz++] = c;
      } else {
        ++myvotes;
        pans += c;
      }
    }
    int dif = goal - myvotes;
    if (dif > 0 && sz >= dif) {
      myvotes += dif;
      nth_element(notmine, notmine + dif - 1, notmine + sz);
      for (int i = 0; i < (dif); ++i) pans += notmine[i];
    }
    if (myvotes >= goal) ans = min(ans, pans);
  }
  cout << ans << endl;
  return 0;
}
