#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 3005 + 5;
long long n, m;
vector<long long> v[MAXN];
long long p[MAXN], c[MAXN];
int main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    v[p[i]].push_back(c[i]);
  }
  for (long long i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
  }
  long long st = v[1].size();
  long long Max = 1ll << 62;
  bool f = 0;
  for (int i = 2; i <= m; i++) {
    if (v[i].size() >= st) f = 1;
  }
  if (!f) {
    cout << 0 << endl;
    return 0;
  }
  for (long long i = st; i <= n; i++) {
    long long val = 0, num = 0, mm = 0;
    priority_queue<long long, vector<long long>, greater<long long> > q;
    for (long long j = 2; j <= m; j++) {
      long long len = v[j].size();
      if (len > i) {
        long long dis = len - i;
        for (long long k = 0; k < len; k++) {
          long long x = v[j][k];
          if (k < dis)
            val += x, num++;
          else
            q.push(x);
        }
      } else {
        for (long long k = 0; k < len; k++) {
          q.push(v[j][k]);
        }
      }
    }
    if (num + st <= i) {
      long long dis = i - (num + st) + 1;
      while (q.size() && dis) {
        long long x = q.top();
        q.pop();
        val += x;
        dis--;
      }
    }
    Max = min(Max, val);
  }
  cout << Max << endl;
}
