#include <bits/stdc++.h>
using namespace std;
long long ans;
int a[3010][3010], n, m, b[3010];
inline long long Work(int x) {
  int nw = a[1][0];
  long long nm = 0;
  for (int i = 2; i <= m; i++)
    for (int j = 1; j <= a[i][0] - x + 1; j++) nm += a[i][j], nw++;
  int tot = 0;
  for (int i = 2; i <= m; i++)
    for (int j = max(a[i][0] - x + 2, 1); j <= a[i][0]; j++) b[++tot] = a[i][j];
  sort(b + 1, b + 1 + tot);
  for (int i = 1; i <= x - nw; i++) nm += b[i];
  return (nm);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x][++a[x][0]] = y;
  }
  for (int i = 1; i <= m; i++) sort(a[i] + 1, a[i] + 1 + a[i][0]);
  ans = 1e16;
  for (int i = a[1][0]; i <= n; i++) {
    ans = min(ans, Work(i));
  }
  printf("%I64d\n", ans);
}
