#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[3001];
long long c[3001];
vector<long long> vec[3005];
long long sum[3005][3005];
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%lld", &p[i], &c[i]);
    p[i]--;
    vec[p[i]].push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    sort(vec[i].begin(), vec[i].end());
  }
  long long res = 1LL << 60;
  for (int i = 1; i <= n; i++) {
    int ct = vec[0].size();
    long long vc = 0;
    priority_queue<long long, vector<long long>, greater<long long> > que;
    for (int j = 1; j < m; j++) {
      for (int k = 0; k < vec[j].size(); k++) {
        int rest = vec[j].size() - k;
        if (rest >= i) {
          ct++;
          vc += vec[j][k];
        } else {
          que.push(vec[j][k]);
        }
      }
    }
    while (ct < i) {
      ct++;
      vc += que.top();
      que.pop();
    }
    res = min(res, vc);
  }
  printf("%lld\n", res);
  return 0;
}
