#include <bits/stdc++.h>
using namespace std;
using namespace std;
bool vis[4000];
struct peo {
  int id;
  int cost;
  peo(int id, int cost) : id(id), cost(cost) {}
};
vector<peo> G[4000];
vector<peo> a;
int n, m;
bool cmp(peo &a, peo &b) { return a.cost < b.cost; }
int main() {
  cin >> n >> m;
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    int party, cost;
    cin >> party >> cost;
    if (party != 1) {
      a.push_back(peo(i, cost));
      G[party].push_back(peo(i, cost));
    } else {
      ++sum;
    }
  }
  sort(a.begin(), a.end(), cmp);
  for (int i = 1; i <= m; ++i) {
    sort(G[i].begin(), G[i].end(), cmp);
  }
  long long mincost = static_cast<long long int>(1e18 + 7);
  for (int k = 0; k < n; ++k) {
    memset(vis, 0, sizeof(vis));
    long long ans = 0;
    int t = 0;
    for (int i = 2; i <= m; ++i) {
      int newTicket = 0;
      while (G[i].size() - newTicket > k) {
        ans += G[i][newTicket].cost;
        vis[G[i][newTicket].id] = 1;
        ++newTicket;
      }
      t += newTicket;
    }
    int pos = 0;
    while (t + sum <= k) {
      if (!vis[a[pos].id]) {
        ans += a[pos].cost;
        ++t;
        vis[a[pos].id] = 1;
      }
      ++pos;
    }
    mincost = min(mincost, ans);
  }
  printf("%I64d\n", mincost);
  return 0;
}
