#include <bits/stdc++.h>
using namespace std;
inline long long getnum() {
  char c = getchar();
  long long num, sign = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') sign = -1;
  for (num = 0; c >= '0' && c <= '9';) {
    c -= '0';
    num = num * 10 + c;
    c = getchar();
  }
  return num * sign;
}
long long A[3004], C[3004], B[1004];
vector<long long> V[3004], W[3004];
priority_queue<int, vector<int>, greater<int>> PQ;
int main() {
  int n = getnum(), m = getnum();
  for (int i = 1; i <= n; i++) {
    A[i] = getnum();
    B[A[i]]++;
    C[i] = getnum();
    V[A[i]].push_back(C[i]);
    W[A[i]].push_back(C[i]);
  }
  for (int i = 1; i <= m; i++) {
    sort(V[i].begin(), V[i].end());
    sort(W[i].begin(), W[i].end());
    for (int j = 1; j < V[i].size(); j++) V[i][j] += V[i][j - 1];
  }
  long long ans = LLONG_MAX;
  int l = 1, r = n;
  for (int mid = 1; mid <= n; mid++) {
    int can = B[1];
    long long sum = 0;
    for (int i = 2; i <= m; i++) {
      for (int j = 0, rem = W[i].size(); j < W[i].size(); j++) {
        if (rem >= mid)
          sum += W[i][j], can++, rem--;
        else
          PQ.push(W[i][j]);
      }
    }
    while (can < mid) {
      if (PQ.size() == 0) break;
      int x = PQ.top();
      PQ.pop();
      can++;
      sum += x;
    }
    while (PQ.size()) PQ.pop();
    if (can >= mid) {
      ans = min(ans, sum);
    }
  }
  cout << ans << endl;
}
