#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct peo {
  long long p, c;
} a[3010];
int n, m, ched[3010], num[3010];
long long SUM, tot, ans = INF;
vector<long long> p[3010];
vector<long long> sum[3010];
bool cmp(peo x, peo y) { return x.c < y.c; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d%I64d", &a[i].p, &a[i].c);
    num[a[i].p]++;
    if (a[i].p == 1) {
      n--;
      i--;
      continue;
    }
    p[a[i].p].push_back(a[i].c);
  }
  if (num[1] > n) {
    puts("0");
    return 0;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 2; i <= m; i++) {
    if (p[i].size() == 0) continue;
    sort(p[i].begin(), p[i].end());
    sum[i].push_back(p[i][0]);
    for (int j = 1; j < (int)p[i].size(); j++) {
      sum[i].push_back(sum[i][j - 1] + p[i][j]);
    }
  }
  for (int i = num[1]; i <= n + num[1]; i++) {
    if (i == 0) continue;
    SUM = 0;
    tot = 0;
    for (int j = 2; j <= m; j++) {
      if (num[j] >= i) {
        ched[j] = num[j] - i + 1;
        tot += ched[j];
        SUM += sum[j][ched[j] - 1];
      }
    }
    if (tot > i - num[1]) {
      goto END;
    } else if (tot < i - num[1]) {
      for (int j = 1; j <= n && tot < (i - num[1]); j++) {
        if (!ched[a[j].p]) {
          tot++;
          SUM += a[j].c;
        } else {
          ched[a[j].p]--;
        }
      }
    }
    if (tot == i - num[1]) ans = min(ans, SUM);
  END:
    memset(ched, 0, sizeof(ched));
  }
  printf("%I64d", ans);
  return 0;
}
