#include <bits/stdc++.h>
using namespace std;
long long n, m;
pair<long long, long long> a[3005];
long long v[3005];
vector<long long> vec[3005];
priority_queue<long long, vector<long long>, greater<long long> > mc;
bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  return a > b;
}
int main() {
  cin >> n >> m;
  if (m == 1) {
    cout << 0 << endl;
    exit(0);
  };
  long long x, y;
  for (long long i = 0; i < n; i++) {
    scanf("%I64d%I64d", &x, &y);
    x--;
    v[x]++;
    vec[x].push_back(y);
    if (x) mc.push(y);
  }
  for (long long i = 0; i < m; i++) {
    a[i].first = v[i];
    a[i].second = i;
    sort(vec[i].begin(), vec[i].end());
  }
  sort(a, a + m, compare);
  if (a[0].second == 0 && a[0].first > a[1].first) {
    cout << 0 << endl;
    exit(0);
  };
  long long need = a[0].first - v[0] + 1;
  long long tneed;
  priority_queue<long long, vector<long long>, greater<long long> > tmc;
  long long res = 541885418851154LL;
  long long cost;
  for (long long i = 0; i <= a[0].first; i++) {
    cost = 0;
    while (tmc.size()) {
      tmc.pop();
    }
    tneed = need;
    if (tneed <= 0) {
      res = 0;
      break;
    }
    long long debug = (i == 2);
    for (long long j = 0; j < m; j++) {
      long long tj = a[j].second;
      for (long long k = max(v[tj] - i, 0LL); k < v[tj]; k++) {
        if (tj != 0) tmc.push(vec[tj][k]);
      }
      if (v[tj] <= i) continue;
      for (long long k = 0; k < v[tj] - i; k++) {
        cost += vec[tj][k];
        tneed--;
        if (tneed == 0) break;
      }
      if (tneed == 0) break;
    }
    tneed -= a[0].first - i;
    if (tneed <= 0) {
      res = min(res, cost);
      continue;
    }
    for (long long j = 0; j < tneed; j++) {
      cost += tmc.top();
      tmc.pop();
    }
    res = min(res, cost);
  }
  cout << res << endl;
  return 0;
}
