#include <bits/stdc++.h>
using namespace std;
vector<int> v[3005];
vector<long long> sum[3005];
struct node {
  int mon;
  int who;
  bool operator<(const node &a) const { return mon > a.mon; }
};
int main() {
  int n, m;
  int p, c;
  int get;
  int Max;
  long long ans;
  int pos[3005];
  while (~scanf("%d%d", &n, &m)) {
    Max = 0;
    get = 0;
    for (int i = 2; i <= m; i++) {
      v[i].clear();
      sum[i].clear();
    }
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &p, &c);
      if (p == 1)
        get++;
      else {
        v[p].push_back(c);
      }
    }
    for (int i = 2; i <= m; i++) {
      long long s = 0;
      sort(v[i].begin(), v[i].end());
      Max = max(Max, (int)v[i].size());
      for (int j = 0; j < v[i].size(); j++) {
        s += v[i][j];
        sum[i].push_back(s);
      }
    }
    if (get > Max) {
      printf("0\n");
      continue;
    } else {
      int g;
      ans = 999999999999999L;
      for (int i = get; i <= Max; i++) {
        g = get;
        long long cost = 0;
        memset(pos, 0, sizeof(pos));
        for (int j = 2; j <= m; j++) {
          if (i < v[j].size()) {
            pos[j] = v[j].size() - i;
            g += pos[j];
            cost += sum[j][pos[j] - 1];
          }
        }
        if (g > i) {
          ans = min(ans, cost);
          continue;
        }
        priority_queue<node> Q;
        for (int j = 2; j <= m; j++) {
          if (pos[j] < v[j].size()) Q.push((node){v[j][pos[j]], j});
        }
        int need = i + 1 - g;
        while (need) {
          if (Q.empty()) break;
          node a = Q.top();
          Q.pop();
          cost += a.mon;
          pos[a.who]++;
          if (v[a.who].size() > pos[a.who])
            Q.push((node){v[a.who][pos[a.who]], a.who});
          need--;
        }
        if (need == 0) ans = min(ans, cost);
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
