#include <bits/stdc++.h>
using namespace std;
const int maxN = 3005;
const long long inf = (long long)1e15;
int n, m;
int p[maxN], c[maxN];
vector<long long> val[maxN];
vector<int> que[maxN];
struct Node {
  int x, y;
  Node() {}
  Node(int _x, int _y) { x = _x, y = _y; }
  bool operator<(const Node& t) const {
    return que[x][y] < que[t.x][t.y] || que[x][y] == que[t.x][t.y] && x < t.x;
  }
};
set<Node> S;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", p + i, c + i);
    que[p[i]].push_back(c[i]);
    val[p[i]].push_back(0);
  }
  for (int i = 1; i <= m; i++)
    if ((int((que[i]).size()))) {
      sort(que[i].begin(), que[i].end());
      val[i][0] = que[i][0];
      for (int j = 1; j < (int((que[i]).size())); j++) {
        val[i][j] = val[i][j - 1] + que[i][j];
      }
    }
  long long ans = inf;
  for (int i = max(1, (int((que[1]).size()))); i <= n; i++) {
    long long res = 0;
    int cnt = 0;
    S.clear();
    for (int j = 2; j <= m; j++) {
      if ((int((que[j]).size())) >= i) {
        int k = (int((que[j]).size())) - i + 1;
        res += val[j][k - 1];
        cnt += k;
        if (k < (int((que[j]).size()))) S.insert(Node(j, k));
      } else {
        if ((int((que[j]).size()))) S.insert(Node(j, 0));
      }
    }
    int need = i - (int((que[1]).size())) - cnt;
    for (; need > 0; need--) {
      assert((int((S).size())) > 0);
      int x = S.begin()->x;
      int y = S.begin()->y;
      S.erase(S.begin());
      res += que[x][y];
      if (y + 1 < (int((que[x]).size()))) {
        S.insert(Node(x, y + 1));
      }
    }
    ans = min(ans, res);
  }
  printf("%I64d\n", ans);
  return 0;
}
