#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
struct acm {
  long long p, c;
} a[3010];
int n, m, b[3010], flag;
long long ans;
bool cmp(acm a, acm b) { return a.c > b.c; }
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &a[i].p, &a[i].c);
    b[a[i].p]++;
  }
  flag = 0;
  for (int i = 2; i <= m; i++) {
    if (b[i] >= b[1]) flag++;
  }
  if (flag == 0) {
    printf("0");
    return 0;
  }
  sort(a + 1, a + 1 + n, cmp);
  ans = INF;
  for (int i = b[1]; i <= n; i++) {
    int cnt = n - i;
    long long ss = 0;
    memset(b, 0, sizeof(b));
    for (int j = 1; j <= n; j++) {
      if (a[j].p != 1 && cnt > 0) {
        if (b[a[j].p] + 1 < i) {
          b[a[j].p]++;
          cnt--;
        } else
          ss += a[j].c;
      } else if (a[j].p != 1)
        ss += a[j].c;
    }
    ans = min(ans, ss);
  }
  printf("%lld", ans);
}
