#include <bits/stdc++.h>
using namespace std;
struct Node {
  int rol, col;
  long long v;
  Node(int rol = 0, int col = 0, long long v = 0) : rol(rol), col(col), v(v) {}
  bool operator<(const Node& n1) const { return v > n1.v; }
};
int n, m;
vector<int> zd[3007];
int vis[3007];
long long sum[3007][3007];
long long ans = -1;
int h[3007];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int p, c;
    cin >> p >> c;
    zd[p].push_back(c);
  }
  int maxid = 1;
  for (int i = 1; i <= m; i++) {
    sort(zd[i].begin(), zd[i].end());
    if (zd[i].size() >= zd[maxid].size()) maxid = i;
    for (int j = 0; j < zd[i].size(); j++) {
      if (j == 0)
        sum[i][0] = zd[i][0];
      else
        sum[i][j] = sum[i][j - 1] + zd[i][j];
    }
  }
  if (maxid == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i <= n; i++) {
    memset(h, 0, sizeof(h));
    long long ansn = 0;
    int cnt = 0;
    for (int j = 2; j <= m; j++) {
      if (zd[j].size() <= i) {
        continue;
      }
      ansn += sum[j][zd[j].size() - i - 1];
      cnt += zd[j].size() - i;
      h[j] = zd[j].size() - i;
    }
    if (cnt + zd[1].size() <= i) {
      int k = i - (cnt + zd[1].size()) + 1;
      priority_queue<Node> q;
      for (int j = 2; j <= m; j++) {
        if (h[j] < zd[j].size()) {
          q.push(Node(j, h[j], zd[j][h[j]]));
        }
      }
      while (k) {
        Node x = q.top();
        q.pop();
        ansn += x.v;
        if (x.col + 1 < zd[x.rol].size()) {
          q.push(Node(x.rol, x.col + 1, zd[x.rol][x.col + 1]));
        }
        k--;
      }
    }
    if (ansn < ans || ans == -1) {
      ans = ansn;
    }
  }
  cout << ans;
  return 0;
}
