#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 5;
const int maxm = 3000 + 5;
int n, m;
int p[maxn], c[maxn];
vector<int> z[maxm];
int cnt[maxm];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i] >> c[i];
    z[p[i]].push_back(c[i]);
  }
  for (int i = 1; i <= m; ++i) {
    sort(z[i].begin(), z[i].end());
  }
  long long ans = 1e18;
  for (int i = 1; i <= n; ++i) {
    long long sum = 0;
    memset(cnt, 0, sizeof(cnt));
    int mine = z[1].size();
    priority_queue<int> pq;
    for (int j = 1; j <= m; ++j) {
      cnt[j] = z[j].size();
      if (j == 1) {
        continue;
      }
      int take = max(0, (int)z[j].size() - i + 1);
      for (int k = 0; k < take; ++k) {
        sum += z[j][k];
      }
      cnt[j] -= take;
      for (int k = take; k < z[j].size(); ++k) {
        pq.push(-z[j][k]);
      }
      mine += take;
    }
    while (mine < i) {
      sum -= pq.top();
      pq.pop();
      ++mine;
    }
    ans = min(ans, sum);
  }
  cout << ans << '\n';
  return 0;
}
