#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
ostream &operator<<(ostream &o, const vector<T> &v) {
  for (size_t i = 0; i < v.size(); i++)
    o << v[i] << (i + 1 != v.size() ? " " : "");
  return o;
}
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : ", ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T>
ostream &operator<<(ostream &o, tuple<T...> const &t) {
  o << "(";
  _ot<0>(o, t);
  o << ")";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, pair<T, U> const &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
int a[3000], b[3000];
vector<int> v[3001];
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n, m;
  cin >> m >> n;
  ll ans = 1e18;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    v[a[i]].emplace_back(b[i]);
  }
  for (int j = 0; j <= n; j++) {
    sort(begin(v[j]), end(v[j]));
  }
  for (int i = max(1, (int)v[0].size()); i <= m; i++) {
    (42);
    ll res = 0;
    int cnt = v[0].size();
    vector<int> rest;
    for (int j = 1; j < n; j++) {
      for (int k = 0; k < (int)v[j].size() - (i - 1); k++) {
        res += v[j][k];
        cnt++;
      }
      for (int k = max(0, (int)v[j].size() - (i - 1)); k < (int)v[j].size();
           k++) {
        rest.emplace_back(v[j][k]);
      }
    }
    sort(begin(rest), end(rest));
    int t = 0;
    while (cnt < i) {
      res += rest[t];
      cnt++;
      t++;
    }
    ans = min(ans, res);
  }
  cout << ans << endl;
  return 0;
}
