#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e3 + 5;
const long long INF = 1e18;
int N, M, L, R;
long long H, S;
int P[MAXN];
int K[MAXN];
pair<int, int> C[MAXN];
bool B[MAXN];
vector<int> V[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    cin >> P[i] >> C[i].first;
    C[i].second = i;
  }
  sort(C + 1, C + N + 1);
  for (int i = N; i >= 1; i--) {
    if (P[C[i].second] == 1) {
      B[i] = 1;
    } else {
      S += C[i].first;
      K[P[C[i].second]]++;
      V[K[P[C[i].second]]].push_back(i);
    }
  }
  L = N;
  R = N;
  H = S;
  for (int i = N; i >= 1; i--) {
    for (int j : V[i]) {
      if (j > L) {
        S += C[j].first;
        R++;
      } else {
        B[j] = 1;
      }
    }
    while (L && R > i) {
      if (!B[L]) {
        S -= C[L].first;
        R--;
      }
      L--;
    }
    H = min(H, S);
  }
  cout << H << '\n';
  return 0;
}
