#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
int n, m, a[3015], cnt[3015];
long long b[3015];
vector<int> vec[3015];
struct gg {
  long long v;
  int f;
  gg() {}
  gg(long long v, int f) : v(v), f(f) {}
  bool operator<(const gg &g) const { return f == g.f ? v < g.v : f < g.f; }
} g[3015];
long long check(int x) {
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    g[i] = gg(b[i], 0);
    if (1 == a[i]) {
      g[i].f = 1;
    }
  }
  int rem = x - cnt[1];
  for (int i = 2; i <= m; ++i) {
    if (cnt[i] >= x) {
      int del = cnt[i] - x + 1;
      rem -= del;
      for (int j = 0; j < del; ++j) {
        ans += b[vec[i][j]];
        g[vec[i][j]].f = 1;
      }
    }
    if (rem < 0) return -1;
  }
  sort(g + 1, g + n + 1);
  for (int i = 1; i <= rem; ++i) {
    ans += g[i].v;
  }
  return ans;
}
bool cmp(int x, int y) { return b[x] < b[y]; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%lld", &a[i], &b[i]);
    ++cnt[a[i]];
    vec[a[i]].push_back(i);
  }
  for (int i = 1; i <= m; ++i) {
    sort(vec[i].begin(), vec[i].end(), cmp);
  }
  long long ans = 1e18;
  for (int i = max(cnt[1], 1); i <= n; ++i) {
    long long v = check(i);
    if (~v) ans = min(ans, v);
  }
  printf("%lld\n", ans);
  return 0;
}
