#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 15;
const int maxm = 1 << 12;
const int maxe = 4000015;
const int mod = 19260817;
const int inf = 0x3f3f3f3f;
int n, m, q;
vector<pair<int, int> > vec[maxn];
vector<pair<int, int> > sum;
bool vis[maxn];
int a, b;
bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
  scanf("%d%d", &n, &m);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    if (a == 1)
      cnt++;
    else {
      vec[a].push_back(make_pair(i, b));
      sum.push_back(make_pair(i, b));
    }
  }
  for (int i = 2; i <= m; i++) sort(vec[i].begin(), vec[i].end(), cmp);
  sort(sum.begin(), sum.end(), cmp);
  long long ans = 1LL << 60;
  for (int k = max(cnt, 1); k <= n; k++) {
    memset(vis, 0, sizeof(vis));
    long long cntans = 0;
    int cntp = cnt;
    for (int i = 2; i <= m; i++) {
      int id = 0;
      while (vec[i].size() - id >= k && vec[i].size() - id >= cntp) {
        cntans += vec[i][id].second;
        vis[vec[i][id].first] = 1;
        id++;
        cntp++;
      }
    }
    int id = 0;
    while (cntp < k) {
      while (vis[sum[id].first]) id++;
      cntp++;
      cntans += sum[id].second;
      id++;
    }
    ans = min(ans, cntans);
  }
  printf("%lld\n", ans);
  return 0;
}
