#include <bits/stdc++.h>
using namespace std;
const int MAX = 3010;
int N, M;
class Node {
 public:
  int p;
  long long c;
};
Node q[MAX];
bool operator<(Node A, Node B) { return A.c < B.c; }
int num[MAX], now[MAX];
bool book[MAX];
long long check(int Max) {
  for (int i = 1; i <= M; ++i) now[i] = num[i];
  memset(book, false, sizeof(book));
  long long sum = 0;
  for (int i = 1; i <= N; ++i) {
    if (q[i].p == 1) {
      book[i] = true;
    } else if (now[q[i].p] < Max)
      continue;
    else {
      now[q[i].p]--;
      sum += q[i].c;
      now[1]++;
      book[i] = true;
    }
  }
  if (now[1] < Max) {
    for (int i = 1; i <= N; ++i) {
      if (now[1] == Max) break;
      if (!book[i]) {
        sum += q[i].c;
        now[1]++;
        book[i] = true;
      }
    }
  }
  return sum;
}
int main(void) {
  cin >> N >> M;
  for (int i = 1; i <= N; ++i) {
    cin >> q[i].p >> q[i].c;
    num[q[i].p]++;
  }
  sort(q + 1, q + 1 + N);
  int l = 1, r = N;
  while (r - l > 3) {
    int mid = (l + r) / 2;
    int rmid = (mid + r) / 2;
    long long sum1 = check(mid);
    long long sum2 = check(rmid);
    if (sum1 > sum2)
      l = mid;
    else
      r = rmid;
  }
  long long res = check(l);
  for (int i = l + 1; i <= r; ++i) {
    res = min(res, check(i));
  }
  cout << res << endl;
  return 0;
}
