#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - 48;
    ch = getchar();
  }
  return x * f;
}
template <typename T>
inline bool Max(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool Min(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
const int N = 3004;
const long long inf = 1e15;
int n, m, fg = 1;
int sz[N], t[N], val[N][N];
long long pre[N][N], ans = inf;
struct data {
  int id, v;
  data() {}
  data(int id, int v) : id(id), v(v) {}
  bool operator<(const data &rhs) const { return rhs.v < v; }
};
priority_queue<data> Q;
int main() {
  n = gi(), m = gi();
  for (int i = 1; i <= n; ++i) {
    int x = gi();
    long long c = gi();
    val[x][++sz[x]] = c;
    fg &= x == 1;
  }
  if (fg) return puts("0"), 0;
  for (int i = 1; i <= m; ++i) {
    sort(val[i] + 1, val[i] + 1 + sz[i]);
    for (int j = 1; j <= sz[i]; ++j) {
      pre[i][j] = pre[i][j - 1] + val[i][j];
    }
  }
  for (int i = max(1, sz[1]); i <= n; ++i) {
    int cnt = i - sz[1];
    long long tmp = 0;
    while (!Q.empty()) Q.pop();
    for (int j = 2; j <= m; ++j) {
      if (sz[j] >= i) {
        tmp += pre[j][sz[j] - i + 1], cnt -= sz[j] - i + 1;
        t[j] = sz[j] - i + 2;
      } else
        t[j] = 1;
      if (t[j] <= sz[j]) Q.push(data(j, val[j][t[j]]));
    }
    for (int j = 1; j <= cnt; ++j) {
      int u = Q.top().id;
      Q.pop();
      tmp += val[u][t[u]++];
      if (t[u] <= sz[u]) Q.push(data(u, val[u][t[u]]));
    }
    Min(ans, tmp);
  }
  printf("%lld\n", ans);
  return 0;
}
