#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
vector<long long> vote[3001];
priority_queue<long long, vector<long long>, greater<long long>> pq;
bool yee(long long a, long long b) { return a > b; }
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n, m, p, c, tmp, cnt, ans = 1e18;
  cin >> n >> m;
  for (long long i = 0; i < n; ++i) cin >> p >> c, vote[p].push_back(c);
  for (long long i = 1; i <= m; ++i) sort(vote[i].begin(), vote[i].end(), yee);
  for (long long i = vote[1].size(); i <= n; ++i) {
    cnt = vote[1].size(), tmp = 0;
    while (!pq.empty()) pq.pop();
    for (long long j = 2, k; j <= m; ++j) {
      for (k = 0; k < vote[j].size() && k < i - 1; ++k) pq.push(vote[j][k]);
      for (; k < vote[j].size(); ++k) tmp += vote[j][k], ++cnt;
    }
    while (cnt < i) tmp += pq.top(), pq.pop(), ++cnt;
    ans = min(ans, tmp);
  }
  cout << ans << "\n";
}
