#include <bits/stdc++.h>
using namespace std;
struct sss {
  int id;
  int cost;
} a[3001];
int f[3001];
int g[3001];
int cmp(struct sss x, struct sss y) { return x.cost < y.cost; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].id, &a[i].cost);
    f[a[i].id]++;
  }
  long long sum = 999999999999, sum2 = 0, ans;
  sort(a + 1, a + n + 1, cmp);
  for (int i = f[1]; i <= n; i++) {
    sum2 = 0;
    ans = 0;
    for (int j = 2; j <= m; j++) {
      if (f[j] >= i) {
        g[j] = f[j] - i + 1;
        sum2 += g[j];
      } else
        g[j] = 0;
    }
    if (i - f[1] < sum2) continue;
    sum2 = i - f[1] - sum2;
    for (int j = 1; j <= n; j++) {
      if (g[a[j].id] != 0) {
        g[a[j].id]--;
        ans += a[j].cost;
      } else {
        if (sum2 != 0 && a[j].id != 1) {
          sum2--;
          ans += a[j].cost;
        }
      }
    }
    sum = min(sum, ans);
  }
  cout << sum;
}
