#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, z, i, j, lt, x, y, d, r, q, k, g;
  vector<long long> a(300001), b(300001), c(300001), tx, ps;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  vector<long long> cst[m + 1];
  for (i = 1; i <= n; i++) {
    cin >> x >> y;
    cst[x].push_back(y);
  }
  for (i = 1; i <= m; i++)
    sort(cst[i].begin(), cst[i].end(), greater<long long>());
  k = cst[1].size();
  d = LLONG_MAX;
  if (k >= n) d = 0;
  for (i = max(1ll, k); i <= n; i++) {
    x = 0;
    y = k;
    vector<long long> tp;
    for (j = 2; j <= m; j++) {
      for (int ii = 0; ii < cst[j].size(); ii++) {
        if (ii >= i - 1) {
          x += cst[j][ii];
          y++;
        } else
          tp.push_back(cst[j][ii]);
      }
    }
    sort(tp.begin(), tp.end());
    for (j = 0; j < tp.size(); j++) {
      if (y >= i) break;
      x += tp[j];
      y++;
    }
    if (y >= i) d = min(d, x);
  }
  cout << d;
}
