#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
const long long MOD = 1e9 + 7;
const double eps = 1e-9;
int n, m;
vector<vector<int> > g;
int main() {
  scanf("%d%d", &n, &m);
  g.resize(m + 1);
  for (int i = 0; i < n; i++) {
    int p, c;
    scanf("%d%d", &p, &c);
    g[p].push_back(c);
  }
  for (int i = 1; i <= m; i++) sort(g[i].begin(), g[i].end());
  long long ans = 1e18;
  for (int k = 1; k <= n; k++) {
    long long cur = 0;
    int x = g[1].size();
    priority_queue<int, vector<int>, greater<int> > s;
    for (int j = 2; j <= m; j++) {
      int l = max(0, (int)g[j].size() - (k - 1));
      for (int i = 0; i < l; i++) cur += g[j][i];
      for (int i = l; i < g[j].size(); i++) s.push(g[j][i]);
      x += l;
    }
    while (x < k && !s.empty()) {
      cur += s.top();
      s.pop();
      x++;
    }
    if (x >= k) ans = min(ans, cur);
  }
  printf("%lld\n", ans);
  return 0;
}
