#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
vector<int> G[N];
int vis[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  long long ans = 1e18;
  for (int i = 0; i < n; i++) {
    int p, c;
    scanf("%d%d", &p, &c);
    G[p].push_back(c);
    vis[p]++;
  }
  int mx = 0;
  for (int i = 1; i <= m; i++) {
    if (vis[i] > mx) {
      mx = vis[i];
    }
  }
  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    if (vis[i] == mx) cnt++;
  }
  if (cnt == 1 && vis[1] == mx) return 0 * puts("0");
  for (int i = 1; i <= m; i++) sort(G[i].begin(), G[i].end());
  for (int i = n; i >= 1; i--) {
    vector<int> r;
    int num = i - vis[1];
    long long sum = 0;
    for (int j = 1; j <= m; j++) {
      if (vis[j] >= i && j != 1) {
        int tmp = vis[j] - i + 1;
        for (int k = 0; k < min(tmp, (int)G[j].size()); k++) {
          r.push_back(G[j][k]);
        }
      }
    }
    for (int j = 0; j < r.size(); j++) {
      num--;
      sum += r[j];
    }
    if (num <= 0) {
      ans = min(ans, sum);
      continue;
    }
    r.clear();
    for (int j = 1; j <= m; j++) {
      if (vis[j] >= i && j != 1) {
        int tmp = vis[j] - i + 1;
        if (tmp >= G[j].size()) continue;
        for (int k = tmp; k < G[j].size(); k++) {
          r.push_back(G[j][k]);
        }
      }
    }
    for (int j = 1; j <= m; j++) {
      if (vis[j] < i && j != 1) {
        for (int k = 0; k < G[j].size(); k++) {
          r.push_back(G[j][k]);
        }
      }
    }
    sort(r.begin(), r.end());
    for (int j = 0; j < r.size(); j++) {
      num--;
      sum += r[j];
      if (num <= 0) break;
    }
    if (num <= 0) ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
