#include <bits/stdc++.h>
using namespace std;
long long n, m, p[5005], c[5005], cnt[5005], sum, ans = 1e17;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    if (p[i] != 1) sum += c[i];
  }
  for (int i = 0; i <= n; i++) {
    long long need = i + 1;
    memset(cnt, 0, sizeof cnt);
    priority_queue<pair<long long, long long> > q;
    for (int j = 1; j <= n; j++)
      if (p[j] != 1) q.push(make_pair(c[j], p[j]));
    long long now = n, use = sum;
    while (now > need && !q.empty()) {
      long long t = q.top().first, id = q.top().second;
      q.pop();
      if (cnt[id] == i) continue;
      cnt[id]++;
      now--;
      use -= t;
    }
    ans = min(ans, use);
  }
  cout << ans << endl;
  return 0;
}
