#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long N = 3000 + 10;
vector<long long> vt[N];
long long a[N];
int main() {
  long long n, m;
  cin >> n >> m;
  long long x, y, sum = 1e18;
  for (long long i = 1; i <= n; i++) {
    cin >> x >> y;
    vt[x].push_back(y);
  }
  bool f = true;
  for (long long i = 2; i <= m; i++)
    if (vt[i].size() >= vt[1].size()) {
      f = false;
      break;
    }
  if (f == true) {
    cout << 0;
    return 0;
  }
  for (long long i = 2; i <= m; i++) sort(vt[i].begin(), vt[i].end());
  for (long long mid = 1; mid <= n; mid++) {
    long long ans = 0;
    memset(a, 0, sizeof(a));
    long long at = 0, cnt = vt[1].size();
    for (long long i = 2; i <= m; i++) {
      long long sz = vt[i].size();
      long long num = sz - mid;
      for (long long j = 0; j < sz; j++) {
        if (j <= num) {
          ans += vt[i][j];
          cnt++;
        } else
          a[at++] = vt[i][j];
      }
    }
    sort(a, a + at);
    long long tp = 0;
    while (cnt < mid) {
      ans += a[tp++];
      cnt++;
    }
    sum = min(sum, ans);
  }
  cout << sum;
  return 0;
}
