#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 3e3 + 10;
const int MAXM = 1400000 + 10;
const long long mod = 1e9 + 7;
bool vis[MAXN];
struct node {
  int p, c, id;
} a[MAXN];
vector<node> vec[MAXN];
bool cmp(node x, node y) { return x.c < y.c; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int now = 0, maxx = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].p, &a[i].c);
    a[i].id = i;
    vec[a[i].p].push_back(a[i]);
    if (a[i].p == 1) now++;
  }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= m; i++) {
    sort(vec[i].begin(), vec[i].end(), cmp);
    if (i != 1) maxx = max(maxx, (int)vec[i].size());
  }
  if (now > maxx) {
    puts("0");
    return 0;
  }
  long long ans = 1e18;
  for (int i = max(1, now); i <= n; i++) {
    long long sum = 0;
    memset((vis), (false), sizeof(vis));
    int temp = now;
    for (int j = 1; j <= m; j++) {
      if (vec[j].size() < i) continue;
      int ned = vec[j].size() - i + 1;
      for (int k = 0; k < ned; k++) {
        sum += (long long)vec[j][k].c;
        temp++;
        vis[vec[j][k].id] = true;
      }
    }
    if (temp < i) {
      for (int j = 1; j <= n && temp < i; j++) {
        if (!vis[a[j].id] && a[j].p != 1) {
          sum += (long long)a[j].c;
          temp++;
        }
      }
    }
    ans = min(ans, sum);
  }
  printf("%lld\n", ans);
  return 0;
}
