#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MX = 3000 + 5;
int n, m;
struct node {
  int p, c;
  bool operator<(const node& A) const { return c < A.c; }
} a[MX];
int num[MX];
long long cost[MX];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].p, &a[i].c);
    num[a[i].p]++;
  }
  sort(a + 1, a + n + 1);
  long long ans = 1e18, sum, cnt;
  for (int i = num[1]; i <= n; i++) {
    sum = 0, cnt = 0;
    for (int j = 2; j <= m; j++) {
      if (num[j] >= i) {
        cost[j] = num[j] - i + 1;
        cnt += cost[j];
      } else
        cost[j] = 0;
    }
    if (i - num[1] < cnt) continue;
    cnt = i - cnt - num[1];
    for (int j = 1; j <= n; j++) {
      if (cost[a[j].p] != 0) {
        cost[a[j].p]--;
        sum += a[j].c;
      } else {
        if (cnt != 0 && a[j].p != 1) {
          cnt--;
          sum += a[j].c;
        }
      }
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
