#include <bits/stdc++.h>
using namespace std;
long long sum[3002] = {};
long long n, m;
vector<long long> v[3002];
long long pre[3002][3002];
int main() {
  cin >> n >> m;
  for (long long i = 0; i <= 3001; i++) {
    for (long long j = 0; j <= 3001; j++) pre[i][j] = 0;
  }
  long long cnt[3002] = {};
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    v[a].push_back(b);
    cnt[a]++;
  }
  for (long long i = 2; i <= m; i++) sort(v[i].begin(), v[i].end());
  for (long long i = 2; i <= m; i++) {
    for (long long j = 0; j < v[i].size(); j++) {
      if (j == 0)
        pre[i][j + 1] = v[i][j];
      else
        pre[i][j + 1] = pre[i][j] + v[i][j];
    }
  }
  long long M = 100000000000000, res;
  for (long long i = max(1LL, cnt[1]); i <= n; i++) {
    long long ans = 0;
    long long y = i - cnt[1];
    long long z = y;
    for (long long j = 2; j <= m; j++) {
      if (v[j].size() >= i) {
        ans = ans + pre[j][v[j].size() - i + 1];
        z = z - (v[j].size() - i + 1);
      }
    }
    if (z > 0) {
      priority_queue<long long, vector<long long>, greater<long long>> pq;
      for (long long j = 2; j <= m; j++) {
        if (v[j].size() >= i) {
          for (long long k = v[j].size() - i + 1; k < v[j].size(); k++)
            pq.push(v[j][k]);
        } else {
          for (long long k = 0; k < v[j].size(); k++) pq.push(v[j][k]);
        }
      }
      long long c = 0;
      while (!pq.empty()) {
        c++;
        long long u = pq.top();
        if (c <= z) ans = ans + u;
        pq.pop();
      }
    }
    if (ans < M) M = ans;
  }
  cout << M;
  return 0;
}
