#include <bits/stdc++.h>
const int MAXN = 3000 + 10;
const int INF = 0x7fffffff;
using namespace std;
int n, m, cost[MAXN], sup[MAXN];
priority_queue<int> Q[MAXN];
long long check(int x) {
  long long res = 0ll;
  for (int i = 1; i <= m; i++)
    while (!Q[i].empty()) Q[i].pop();
  for (int i = 1; i <= n; i++) {
    Q[sup[i]].push(-cost[i]);
  }
  int chan = 0;
  for (int i = 2; i <= m; i++) {
    while (Q[i].size() >= x) {
      res += (long long)-Q[i].top();
      Q[i].pop();
      chan++;
    }
  }
  if (Q[1].size() + chan >= x) return res;
  priority_queue<int> rest;
  while (!rest.empty()) rest.pop();
  for (int i = 2; i <= m; i++) {
    while (!Q[i].empty()) {
      rest.push(Q[i].top());
      Q[i].pop();
    }
  }
  for (int i = 1; i <= (x - (Q[1].size() + chan)); i++) {
    res += (long long)(-rest.top());
    rest.pop();
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &sup[i], &cost[i]);
  }
  long long Ans = check(1);
  for (int i = 2; i <= n; i++) {
    Ans = min(Ans, check(i));
  }
  printf("%lld\n", Ans);
  return 0;
}
