#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 5;
vector<long long> sup[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, p;
  long long c;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> p >> c;
    sup[p].push_back(c);
  }
  for (int i = 1; i <= m; i++) {
    sort(sup[i].begin(), sup[i].end(), greater<long long>());
  }
  long long maxs = max(0, (int)sup[1].size() - 1);
  for (int i = 2; i <= m; i++) {
    maxs = max(maxs, (long long)sup[i].size());
  }
  if (maxs < sup[1].size()) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0, minc = (1ll << 60) - 1;
  for (int i = maxs + 1; i >= 1; i--) {
    priority_queue<long long, vector<long long>, greater<long long> > pq;
    for (int j = 2; j <= m; j++) {
      if (sup[j].size() >= i) {
        ans += sup[j][sup[j].size() - 1];
        sup[j].pop_back();
        sup[1].push_back(1);
      }
      for (int k = 0; k < sup[j].size(); k++) pq.push(sup[j][k]);
    }
    long long aa = ans;
    long long tt = sup[1].size();
    while ((!pq.empty()) && tt < i) {
      tt++;
      aa += pq.top();
      pq.pop();
    }
    minc = min(minc, aa);
  }
  cout << minc << endl;
  return 0;
}
