#include <bits/stdc++.h>
using namespace std;
int N, M, C[3005];
long long ans = 1e18;
vector<pair<int, int> > A[3005];
vector<pair<int, int> > B;
int main() {
  int i, j, k, p, c;
  long long t = 0;
  scanf("%d %d", &N, &M);
  for (i = 1; i <= N; i++) {
    scanf("%d %d", &p, &c);
    A[p].push_back({c, i});
    if (p != 1) B.push_back({c, i});
  }
  for (i = 1; i <= M; i++) sort(A[i].begin(), A[i].end());
  sort(B.begin(), B.end());
  for (i = 1; i <= N; i++) {
    memset(C, 0, sizeof C);
    p = A[1].size();
    t = 0;
    for (j = 2; j <= M; j++) {
      if (A[j].size() >= i) {
        for (k = 0; k <= (int)A[j].size() - i; k++) {
          t += A[j][k].first;
          C[A[j][k].second] = 1;
          p++;
        }
      }
    }
    if (p < i) {
      for (j = 0; j < B.size(); j++) {
        if (!C[B[j].second]) {
          C[B[j].second] = 1;
          t += B[j].first;
          p++;
        }
        if (p >= i) break;
      }
    }
    if (p < i) continue;
    ans = min(ans, t);
  }
  printf("%I64d", ans);
  return 0;
}
