#include <bits/stdc++.h>
using namespace std;
const int N = 3008;
pair<int, int> a[N];
vector<pair<int, int> > b[N];
int nn, n, m, c[N], flg[N];
int main() {
  long long ans = 1ll << 60;
  int i, j, k, s, q;
  long long p;
  scanf("%d%d", &n, &m);
  nn = s = 0;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &j, &k);
    if (j == 1)
      s++;
    else
      c[j]++, a[++nn] = make_pair(k, j);
  }
  sort(a + 1, a + 1 + nn);
  for (i = 1; i <= m; i++) b[i].push_back(make_pair(0, 0));
  for (i = 1; i <= nn; i++) b[a[i].second].push_back(make_pair(a[i].first, i));
  for (i = 0; i <= nn; i++) {
    p = 0;
    q = 0;
    for (j = 1; j <= nn; j++) flg[j] = 0;
    for (j = 2; j <= m; j++)
      if (s + i <= c[j]) {
        k = c[j] - s - i + 1;
        if (k > c[j]) break;
        q += k;
        if (q > i) break;
        for (int pp = 1; pp <= k; pp++) {
          p += b[j][pp].first;
          flg[b[j][pp].second] = 1;
        }
      }
    if (j <= m) continue;
    q = i - q;
    for (j = 1; j <= nn && q; j++)
      if (flg[j] == 0) {
        q--;
        p += a[j].first;
      }
    if (p < ans) ans = p;
  }
  cout << ans;
  return 0;
}
