#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& vec) {
  out << "[";
  for (int x : vec) out << x << ", ";
  out << "]";
  return out;
}
const long long int INF = 1e15;
int n, m;
vector<long long int> ch[3333];
long long int solve(int k) {
  long long int ans = 0;
  int my = ch[1].size() + k;
  int hm = k;
  vector<long long int> extra;
  for (int i = 2; i <= m; i++) {
    int q = max(0, (int)ch[i].size() - my + 1);
    hm -= q;
    for (int j = 0; j < q; j++) ans += ch[i][j];
    for (int j = q; j < (int)ch[i].size(); j++) extra.push_back(ch[i][j]);
  }
  if (hm < 0) return INF;
  sort(extra.begin(), extra.end());
  if ((int)extra.size() < hm) return INF;
  for (int i = 0; i < hm; i++) ans += extra[i];
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int p;
    long long int c;
    cin >> p >> c;
    ch[p].push_back(c);
  }
  for (int i = 1; i <= m; i++) sort(ch[i].begin(), ch[i].end());
  bool ok = true;
  for (int i = 2; i <= m; i++)
    if (ch[i].size() >= ch[1].size()) ok = false;
  if (ok) {
    cout << "0\n";
    return 0;
  }
  long long int ans = INF;
  for (int k = 1; k <= n; k++) ans = min(ans, solve(k));
  cout << ans << '\n';
  return 0;
}
