#include <bits/stdc++.h>
using namespace std;
int n, m;
long long ans;
struct pl {
  int id, num, fl;
} s[3005], e[3005];
int flag[3005], tmp[3005], fl[3005];
bool cmp(const pl &A, const pl &B) {
  return A.id < B.id || A.id == B.id && A.num < B.num;
}
bool cmp2(const pl &A, const pl &B) { return A.num < B.num; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &s[i].id, &s[i].num);
    flag[s[i].id]++;
  }
  sort(s + 1, s + 1 + n, cmp);
  ans = 1e15;
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= n; j++) s[j].fl = 0;
    for (int j = 1; j <= m; j++) tmp[j] = flag[j];
    long long an = 0;
    for (int j = 1; j <= n; j++) {
      if (s[j].id == 1) continue;
      if (tmp[s[j].id] > i)
        tmp[s[j].id]--, tmp[1]++, s[j].fl = 1, an = (long long)an + s[j].num;
    }
    int len = 0;
    for (int j = 1; j <= n; j++) {
      if (s[j].fl) continue;
      e[++len] = s[j];
    }
    sort(e + 1, e + 1 + len, cmp2);
    int l = 0;
    while (tmp[1] <= i && l < len) {
      l++;
      if (e[l].id == 1) continue;
      tmp[1]++;
      an = (long long)an + e[l].num;
    }
    if (tmp[1] > i) ans = min(ans, an);
  }
  cout << ans << endl;
  return 0;
}
