#include <bits/stdc++.h>
using namespace std;
const int N = 3000 + 10;
struct p {
  int id, support, money;
  bool operator<(const p& A) const { return money < A.money; }
} a[N];
vector<p> b[N];
bool used[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].support >> a[i].money;
    a[i].id = i;
    b[a[i].support].push_back(a[i]);
  }
  sort(a + 1, a + n + 1);
  for (int i = 2; i <= m; i++) sort(b[i].begin(), b[i].end());
  long long ans = LLONG_MAX;
  for (int s_max = 0; s_max <= n; s_max++) {
    long long sum = 0;
    int cnt = b[1].size();
    memset(used, 0, sizeof(used));
    for (int i = 2; i <= m; i++) {
      if ((int)b[i].size() <= s_max) continue;
      cnt += (int)b[i].size() - s_max;
      for (int j = 0; j < (int)b[i].size() - s_max; j++) {
        sum += b[i][j].money;
        used[b[i][j].id] = true;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (cnt > s_max) break;
      if (!used[a[i].id] && a[i].support != 1) {
        sum += a[i].money;
        cnt++;
      }
    }
    if (cnt > s_max) {
      if (ans > sum) {
        ans = sum;
      }
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}
