#include <bits/stdc++.h>
using namespace std;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
long long n, m;
long long MIN;
struct node {
  long long id, cost;
  node() {}
  node(long long id, long long cost) : id(id), cost(cost) {}
};
bool cmp(node a, node b) { return a.cost < b.cost; }
vector<node> v[3005];
vector<node> sum;
long long vis[maxn];
void solve(long long x) {
  long long ans = 0;
  long long cnt = v[1].size();
  for (long long i = 0; i < cnt; i++) {
    vis[v[1][i].id] = 1;
  }
  for (long long i = 2; i <= m; i++) {
    long long cha = v[i].size() - x + 1;
    long long pos = 0;
    while (cha > 0) {
      cnt++;
      ans += v[i][pos].cost;
      vis[v[i][pos].id] = 1;
      cha--;
      pos++;
    }
  }
  long long pos = 0;
  while (cnt < x) {
    if (!vis[sum[pos].id]) {
      cnt++;
      ans += sum[pos].cost;
    }
    pos++;
  }
  MIN = min(MIN, ans);
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    node tmp = node(i, b);
    v[a].push_back(tmp);
    sum.push_back(tmp);
  }
  for (long long i = 1; i <= m; i++) sort(v[i].begin(), v[i].end(), cmp);
  sort(sum.begin(), sum.end(), cmp);
  MIN = 1e18;
  for (long long i = 1; i <= n; i++) {
    memset((vis), 0, sizeof(vis));
    solve(i);
  }
  printf("%lld\n", MIN);
  return 0;
}
