#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream& operator<<(ostream& out, pair<S, T> const& p) {
  out << '(' << p.first << ", " << p.second << ')';
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
  long long l = v.size();
  for (long long i = 0; i < l - 1; i++) out << v[i] << ' ';
  if (l > 0) out << v[l - 1];
  return out;
}
template <typename T>
void trace(const char* name, T&& arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}
long long n, m;
const long long N = 3000 + 4;
long long p[N], c[N];
unordered_map<long long, vector<long long> > cnt;
int32_t main() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> p[i] >> c[i];
    cnt[p[i]].push_back(c[i]);
  }
  for (long long i = 2; i <= m; i++) {
    sort(cnt[i].begin(), cnt[i].end());
  }
  long long best = 1000000000000000001;
  for (long long votes = 1; votes <= n; votes++) {
    long long cost = 0;
    long long req = votes - (long long)cnt[1].size();
    vector<long long> remaining;
    for (long long i = 2; i <= m; i++) {
      long long st_idx = max((long long)cnt[i].size() - votes + 1, 0LL);
      for (long long j = 0; j < st_idx; j++) {
        cost += cnt[i][j];
        req--;
      }
      for (long long j = st_idx; j < (long long)cnt[i].size(); j++) {
        remaining.push_back(cnt[i][j]);
      }
    }
    if (req > 0) {
      sort(remaining.begin(), remaining.end());
      for (long long i = 0; i < req; i++) {
        cost += remaining[i];
      }
    }
    best = min(best, cost);
  }
  cout << best << "\n";
  return 0;
}
