#include <bits/stdc++.h>
using namespace std;
long long ans, l;
int n, m, N, i, j, k, s[3005], a[3005][3005], b[3005];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &j, &k);
    a[j][++s[j]] = k;
  }
  for (i = 1; i <= m; i++) sort(a[i] + 1, a[i] + s[i] + 1, greater<int>());
  ans = 1LL << 60;
  for (i = 1; i <= n; i++) {
    l = 0;
    N = 0;
    for (j = 2; j <= m; j++) {
      for (k = 1; k < i && k <= s[j]; k++) b[N++] = a[j][k];
      for (; k <= s[j]; k++) l += a[j][k];
    }
    sort(b, b + N);
    for (j = 0; j < i + N - n; j++) l += b[j];
    ans = min(ans, l);
  }
  cout << ans << endl;
  return 0;
}
