#include <bits/stdc++.h>
using namespace std;
const int N = 3030;
int p[N], c[N], cnt[N];
vector<int> voter[N];
long long pref[N][N];
int rest[N];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> p[i] >> c[i];
    --p[i];
  }
  for (int i = 0; i < n; i++) {
    cnt[p[i]]++;
    voter[p[i]].push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    sort((voter[i]).begin(), (voter[i]).end());
    pref[i][0] = 0;
    for (int j = 0; j < cnt[i]; j++) {
      pref[i][j + 1] = pref[i][j] + voter[i][j];
    }
  }
  long long result = (1LL << 60);
  for (int vote = 1; vote <= n; vote++) {
    long long cost = 0;
    int cur_votes = cnt[0];
    for (int i = 1; i < m; i++) {
      if (cnt[i] >= vote) {
        int to_buy = cnt[i] - vote + 1;
        cost += pref[i][to_buy];
        cur_votes += to_buy;
      }
    }
    if (cur_votes >= vote) {
      result = min(result, cost);
      continue;
    }
    int ptr = 0;
    for (int i = 1; i < m; i++) {
      int idx = 0;
      if (cnt[i] >= vote) {
        idx = cnt[i] - vote + 1;
      }
      for (int j = idx; j < cnt[i]; j++) {
        rest[ptr++] = voter[i][j];
      }
    }
    sort(rest, rest + ptr);
    int need = vote - cur_votes;
    for (int i = 0; i < need; i++) {
      cost += rest[i];
    }
    result = min(result, cost);
  }
  cout << result << endl;
  return 0;
}
