#include <bits/stdc++.h>
using namespace std;
void qmax(int &x, int y) {
  if (x < y) x = y;
}
void qmin(int &x, int y) {
  if (x > y) x = y;
}
inline long long read() {
  char s;
  long long k = 0, base = 1;
  while ((s = getchar()) != '-' && s != EOF && !(isdigit(s)))
    ;
  if (s == EOF) exit(0);
  if (s == '-') base = -1, s = getchar();
  while (isdigit(s)) {
    k = k * 10 + (s ^ '0');
    s = getchar();
  }
  return k * base;
}
inline void write(int x) {
  static char cnt, num[15];
  cnt = 0;
  if (!x) {
    printf("0");
    return;
  }
  for (; x; x /= 10) num[++cnt] = x % 10;
  for (; cnt; putchar(num[cnt--] + 48))
    ;
}
const int inf = 1e9 + 100;
const long long Inf = 1e18;
const int maxn = 3e3 + 100;
int n, m, a[maxn];
vector<int> v[maxn];
bool vis[maxn];
bool cmp(int aa, int bb) { return a[aa] < a[bb]; }
long long calc(int x) {
  long long sum = 0;
  vector<int> b;
  int cnt = v[1].size();
  memset(vis, 0, sizeof(vis));
  for (int i = 2; i <= m; i++)
    for (int j = 0, SZ = v[i].size() - x + 1; j < SZ; j++) {
      sum += a[v[i][j]];
      vis[v[i][j]] = 1;
      cnt++;
    }
  for (int i = 2; i <= m; i++)
    for (int j = 0; j < (int)v[i].size(); j++) {
      if (!vis[v[i][j]]) b.push_back(v[i][j]);
    }
  sort(b.begin(), b.end(), cmp);
  for (int i = 0, SZ = b.size(); i < SZ && cnt < x; i++) {
    sum += a[b[i]], cnt++;
  }
  if (cnt < x) return Inf;
  return sum;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    v[read()].push_back(i);
    a[i] = read();
  }
  for (int i = 1; i <= m; i++) sort(v[i].begin(), v[i].end(), cmp);
  long long ans = Inf;
  for (int i = 1; i <= n; i++) ans = min(ans, calc(i));
  printf("%lld\n", ans);
  return 0;
}
