#include <bits/stdc++.h>
using namespace std;
struct Node {
  int p, c;
  bool operator<(const Node& a) const { return c < a.c; }
} node[3005];
int x[3005];
int vis[3005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) scanf("%d%d", &node[i].p, &node[i].c);
  sort(node, node + n);
  long long ans = 1e14;
  for (int i = 0; i < n; i++) {
    memset(x, 0, sizeof(x));
    memset(vis, 0, sizeof(vis));
    long long sum = 0;
    for (int j = n - 1; j >= 0; j--) {
      if (x[node[j].p] < i || node[j].p == 1) {
        x[node[j].p]++;
      } else {
        sum += node[j].c;
        vis[j] = 1;
        x[1]++;
      }
    }
    for (int j = 0; j < n; j++) {
      if (x[1] <= i && !vis[j] && node[j].p != 1) {
        sum += node[j].c;
        x[1]++;
      }
    }
    if (x[1] > i) ans = min(ans, sum);
  }
  cout << ans << endl;
}
