#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(3)
long long n, m;
struct aa {
  long long x, y;
  bool p;
} a[3005], d[3005], e[3005];
long long b[3005], c[3005][3005];
long long ans = (long long)0x3f3f3f3f * (long long)0x3f3f3f3f, p, rp;
bool cmpa(aa a, aa b) { return (a.x < b.x) || (a.x == b.x) && (a.y < b.y); }
void rd() {
  scanf("%I64d%I64d", &n, &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d%I64d", &a[i].x, &a[i].y);
    if (a[i].x == 1) p++;
  }
  sort(a + 1, a + n + 1, cmpa);
  for (long long i = 1; i <= n; i++) {
    c[a[i].x][b[a[i].x] + 1] = i;
    b[a[i].x]++;
  }
}
long long qing(long long x) {
  long long ans = 0;
  for (long long i = 2; i <= m; i++) {
    for (long long j = 1; j <= b[i] - x; j++) {
      ans += a[c[i][j]].y;
      a[c[i][j]].p = 1;
      rp++;
    }
  }
  return ans;
}
bool cmpb(aa a, aa b) { return (a.p < b.p) || (a.p == b.p) && (a.y < b.y); }
int main() {
  rd();
  long long ls = (long long)0x3f3f3f3f * (long long)0x3f3f3f3f;
  for (long long i = max((long long)1, p); i <= n; i++) {
    long long an = 0;
    rp = 0;
    for (int j = 1; j <= n; j++) a[j].p = 0;
    sort(a + 1, a + n + 1, cmpa);
    an += qing(i - 1);
    sort(a + 1, a + n + 1, cmpb);
    for (long long j = 1; j <= n; j++)
      if (a[j].p != 1 && a[j].x != 1) {
        if (rp == i - p) break;
        an += a[j].y;
        rp++;
      }
    ans = min(ans, an);
    if (an > ans) break;
  }
  cout << ans;
  return 0;
}
