#include <bits/stdc++.h>
using namespace std;
inline int _Int() {
  int x;
  scanf("%d", &x);
  return x;
}
long long int bigMod(long long int A, long long int P, int M) {
  long long int R = 1;
  for (A %= M; P; P >>= 1) {
    if (P & 1) R = (R * A) % M;
    A = (A * A) % M;
  }
  return R;
}
long long int bigMul(long long int A, long long int B, long long int M) {
  long long int R = 0;
  for (A %= M; B; B >>= 1) {
    if (B & 1) R = (R + A) % M;
    A = (A + A) % M;
  }
  return R;
}
long long int negMod(long long int A, long long int B) {
  return (((A % B) + B) % B);
}
long long int invMod(long long int A, long long int M) {
  return bigMod(A, M - 2, M);
}
unsigned long long int _pow(unsigned long long int A, int P) {
  unsigned long long int R = 1;
  for (; P; P >>= 1) {
    if (P & 1) R = (R * A);
    A = (A * A);
  }
  return R;
}
template <class T>
T GCD(T x, T y) {
  while (x) x ^= y ^= x ^= y %= x;
  return y;
}
template <class T>
bool inRng(T u, T v, T x) {
  return u <= x && x <= v;
}
const int M = 3000 + 7;
int n, m;
struct data {
  vector<int> votes[M];
};
long long int winWith(int numberOfVotes, data DATA) {
  int initialVoteCount = DATA.votes[1].size();
  if (numberOfVotes < initialVoteCount) return -1;
  long long int minCost = 0;
  vector<int> rest;
  for (int i = 2; i < m + 1; i++) {
    while (DATA.votes[i].size() >= numberOfVotes) {
      minCost += DATA.votes[i].back();
      DATA.votes[i].pop_back();
      initialVoteCount++;
    }
    while (DATA.votes[i].size()) {
      rest.push_back(DATA.votes[i].back());
      DATA.votes[i].pop_back();
    }
  }
  sort(rest.begin(), rest.end(), greater<int>());
  if (numberOfVotes < initialVoteCount) return -1;
  while (initialVoteCount != numberOfVotes) {
    initialVoteCount++;
    minCost += rest.back();
    rest.pop_back();
  }
  return minCost;
}
void Main() {
  n = _Int();
  m = _Int();
  data input;
  long long int Ans = 0;
  for (int i = 0; i < n; i++) {
    int preferedPerson = _Int();
    int price = _Int();
    input.votes[preferedPerson].push_back(price);
    Ans += price;
  }
  for (int i = 1; i < m + 1; i++) {
    sort(input.votes[i].begin(), input.votes[i].end(), greater<int>());
  }
  for (int i = 1; i < n + 1; i++) {
    long long int temp = winWith(i, input);
    if (temp != -1) Ans = min(Ans, temp);
  }
  printf("%lld\n", Ans);
}
int main() {
  Main();
  return 0;
}
