#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
long long mod = 1e9 + 7;
long long fmod(long long b, long long exp) {
  long long res = 1;
  while (exp) {
    if (exp & 1ll) res = (res * b) % mod;
    b = (b * b) % mod;
    exp /= 2ll;
  }
  return res;
}
multiset<long long> st[N];
multiset<long long> fin;
int p[N];
long long c[N], A[N];
long long INF = 1e18 + 3;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1, n, i, j;
  int m;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    st[p[i]].insert(c[i]);
    if (p[i] != 1) fin.insert(c[i]);
  }
  int cur = st[1].size();
  long long cost = 0;
  int len = 0;
  long long mn = INF;
  for (int mid = n; mid >= 1; mid--) {
    for (int i = 2; i <= m; i++) {
      if (!st[i].size()) continue;
      while (st[i].size() >= mid) {
        cur++;
        int tp = *(st[i].begin());
        cost += 1ll * tp;
        st[i].erase(st[i].begin());
        fin.erase(fin.find(tp));
      }
    }
    long long cur_cost = cost;
    int cur_num = cur;
    len = 0;
    while (cur_num < mid) {
      int tp = *(fin.begin());
      fin.erase(fin.begin());
      A[++len] = tp;
      cur_cost += 1ll * tp;
      cur_num++;
    }
    mn = min(mn, cur_cost);
    for (int i = 1; i <= len; i++) fin.insert(A[i]);
  }
  if (mn == INF)
    cout << "0\n";
  else
    cout << mn << "\n";
  return 0;
}
