#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
struct d_ {
  template <typename T>
  d_& operator,(const T& x) {
    cerr << ' ' << x;
    return *this;
  }
  template <typename T>
  d_& operator,(const vector<T>& x) {
    for (auto x : x) cerr << ' ' << x;
    return *this;
  }
} d_t;
int main(void) {
  ios_base::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  vll p(n), c(n);
  vector<vpll> vot(m);
  for (ll i(0); i < n; i++) {
    cin >> p[i] >> c[i];
    p[i]--;
    vot[p[i]].push_back({c[i], i});
  }
  for (ll i(0); i < m; i++) sort(begin(vot[i]), end(vot[i]));
  ll ret = (1LL << 61);
  for (ll win = max(ll(vot[0].size()), 1ll); win <= n; win++) {
    vll cur(n);
    for (ll i(0); i < n; i++) {
      cur[i] = c[i];
      if (p[i] == 0) c[i] = (1LL << 61);
    }
    ll curret = 0;
    ll have = vot[0].size();
    for (ll i(1); i < m; i++)
      if (vot[i].size() >= win) {
        ll need = vot[i].size() - win + 1;
        for (ll j(0); j < need; j++) {
          pll th = vot[i][j];
          curret += th.first;
          cur[th.second] = (1LL << 61);
          have++;
        }
      }
    sort(begin(cur), end(cur));
    for (ll i(0); i < win - have; i++) {
      curret += cur[i];
    }
    ret = min(ret, curret);
  }
  cout << ret << endl;
  return 0;
}
