#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int n, m, cnt[3009], p, c, cn[3009];
long long ans = 1e17;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> p >> c;
    if (p != 1) v.push_back({c, p});
    cnt[p]++;
  }
  sort(v.rbegin(), v.rend());
  for (int i = 1; i <= n; i++) {
    vector<pair<int, int> > vot = v;
    vector<pair<int, int> > mn;
    long long sum = 0;
    for (int j = 1; j <= m; j++) cn[j] = cnt[j];
    while (!vot.empty()) {
      p = vot.back().second, c = vot.back().first;
      vot.pop_back();
      if (cn[p] >= i) {
        sum += c;
        cn[p]--;
        cn[1]++;
      } else {
        mn.push_back({c, p});
      }
    }
    if (cn[1] >= i) {
      ans = min(ans, sum);
      continue;
    }
    for (auto j : mn) {
      sum += j.first;
      cn[1]++;
      if (cn[1] >= i) break;
    }
    ans = min(ans, sum);
  }
  cout << ans;
}
