#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int maxn = 3030;
int n, m, W;
vector<int> f[maxn];
vector<int> tmp;
int p[maxn], w[maxn];
LL gao(int vote) {
  LL ret = 0;
  tmp.clear();
  int need = vote - W;
  for (int i = 2; i <= m; i++) {
    int id = 0;
    if (f[i].size() >= vote) {
      id = f[i].size() - vote + 1;
    }
    if (id > need) return 1E18;
    need -= id;
    for (int j = 0; j < id; j++) {
      ret += f[i][j];
    }
    for (int j = id; j < f[i].size(); j++) {
      tmp.push_back(f[i][j]);
    }
  }
  sort(tmp.begin(), tmp.end());
  for (int i = 0; i < need; i++) {
    ret += tmp[i];
  }
  return ret;
}
signed main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++) f[i].clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &p[i], &w[i]);
      f[p[i]].push_back(w[i]);
    }
    for (int i = 1; i <= m; i++) {
      sort(f[i].begin(), f[i].end());
    }
    LL ret = 1E18;
    W = f[1].size();
    for (int i = W; i <= n; i++) {
      ret = min(ret, gao(i));
    }
    printf("%lld\n", ret);
  }
  return 0;
}
