#include <bits/stdc++.h>
using namespace std;
int n, m, p[3010], c[3010];
vector<pair<int, int> > a[3010];
int A[3010];
bool del[3010];
bool cmp(int a, int b) { return c[a] < c[b]; }
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> p[i] >> c[i];
    a[p[i]].push_back(make_pair(c[i], i));
    A[i] = i;
  }
  for (int i = 2; i <= m; i++) sort(a[i].begin(), a[i].end());
  sort(A, A + n, cmp);
  long long ans = 1ll << 62;
  for (int k = 0; k <= n - a[1].size(); k++) {
    int l = a[1].size() + k - 1;
    long long s = 0;
    for (int i = 0; i < n; i++) del[i] = 0;
    int tot = 0;
    if (l < 0) continue;
    for (int i = 2; i <= m; i++) {
      if (a[i].size() > l) {
        for (int j = 0; j < a[i].size() - l; j++) {
          s += a[i][j].first;
          del[a[i][j].second] = 1;
          tot++;
        }
      }
    }
    for (int i = 0; i < n && tot < k; i++)
      if (!del[A[i]] && p[A[i]] != 1) {
        s += c[A[i]];
        tot++;
      }
    if (s < ans) ans = s;
  }
  cout << ans << endl;
  return 0;
}
