#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
struct node {
  int num;
  int value;
} a[maxn];
int bucket[maxn];
int ticket[maxn];
int cmp(node a, node b) { return a.value < b.value; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a[i].num, &a[i].value);
    bucket[a[i].num]++;
  }
  sort(a, a + n, cmp);
  long long min_ans = 1e16, sum = 0, ans;
  for (int i = bucket[1]; i <= n; i++) {
    sum = 0;
    ans = 0;
    for (int j = 2; j <= m; j++) {
      if (bucket[j] >= i) {
        ticket[j] = bucket[j] - i + 1;
        sum += ticket[j];
      } else
        ticket[j] = 0;
    }
    if (i - ticket[1] < sum) continue;
    sum = i - bucket[1] - sum;
    for (int k = 0; k <= n; k++) {
      if (ticket[a[k].num] != 0) {
        ans += a[k].value;
        ticket[a[k].num]--;
      } else if (sum > 0 && a[k].num != 1) {
        ans += a[k].value;
        sum--;
      }
    }
    min_ans = min(min_ans, ans);
  }
  printf("%lld", min_ans);
  return 0;
}
