#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
int p[N], c[N];
vector<int> v[N];
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    v[p[i]].push_back(c[i]);
  }
  for (int i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
    reverse(v[i].begin(), v[i].end());
  }
  long long ans = 1e18;
  for (int k = 0; k <= n - 1; k++) {
    long long sum = 0;
    long long cnt = v[1].size();
    multiset<int> fr;
    for (int i = 2; i <= m; i++) {
      for (int j = 0; j < min((int)v[i].size(), k); j++) fr.insert(v[i][j]);
      for (int j = k; j < v[i].size(); j++) {
        sum += v[i][j];
        cnt++;
      }
    }
    while (cnt <= k) {
      sum += *fr.begin();
      cnt++;
      fr.erase(fr.begin());
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
}
int main() {
  solve();
  (void)0;
  return 0;
}
