#include <bits/stdc++.h>
using namespace std;
using cat = long long;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N, M;
  cin >> N >> M;
  vector<vector<int> > V(M);
  for (int i = 0; i < N; i++) {
    int x, c;
    cin >> x >> c;
    V[x - 1].push_back(c);
  }
  for (int i = 0; i < M; i++) {
    sort(begin(V[i]), end(V[i]));
    reverse(begin(V[i]), end(V[i]));
  }
  cat ans = 1234567890123456789LL;
  for (int w = 1; w <= N; w++) {
    cat cur = 0;
    int votes = V[0].size();
    vector<int> C;
    for (int i = 1; i < M; i++) {
      for (int j = w - 1; j < (int)V[i].size(); j++) cur += V[i][j], votes++;
      for (int j = 0; j < min((int)V[i].size(), w - 1); j++)
        C.push_back(V[i][j]);
    }
    if (votes < w) {
      sort(begin(C), end(C));
      if ((int)C.size() + votes < w) continue;
      for (int i = 0; i < w - votes; i++) cur += C[i];
    }
    ans = min(ans, cur);
  }
  cout << ans << "\n";
  return 0;
}
