#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 3005;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
int N, M;
ii arr[MAXN];
bool done[MAXN];
vector<ii> partai[MAXN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    arr[i] = {y, x};
  }
  sort(arr, arr + N);
  for (int i = 0; i < N; i++) {
    partai[arr[i].b].push_back({arr[i].a, i});
  }
  long long ans = INF;
  for (int i = N; i >= 1; i--) {
    memset(done, 0, sizeof(done));
    int cnt = 0;
    long long cost = 0;
    for (int j = 2; j <= M; j++) {
      for (int k = 0; k < (int)partai[j].size() - i + 1; k++) {
        cost += partai[j][k].a;
        done[partai[j][k].b] = 1;
        cnt++;
      }
    }
    for (int j = 0; j < N && (int)partai[1].size() + cnt < i; j++) {
      if (done[j] || arr[j].b == 1) continue;
      cost += arr[j].a;
      cnt++;
    }
    ans = min(ans, cost);
  }
  cout << ans << "\n";
  return 0;
}
