#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > util[m];
  vector<pair<long long, long long> > other;
  long long p[n];
  long long c[n];
  for (long long i = 0; i < n; i++) {
    cin >> p[i] >> c[i];
    p[i]--;
    util[p[i]].push_back(make_pair(c[i], i));
    if (p[i] != 0) other.push_back(make_pair(c[i], i));
  }
  long long check = 0;
  for (long long i = 1; i < m; i++) {
    sort(util[i].begin(), util[i].end());
    if (util[i].size() >= util[0].size()) check++;
  }
  sort(other.begin(), other.end());
  long long v = util[0].size();
  if (check == 0) {
    cout << 0;
    return 0;
  }
  long long res = -1;
  vector<bool> changed;
  long long start = 1;
  start = max(start, v);
  for (long long k = start; k <= (n / 2) + 1; k++) {
    long long temp = 0;
    long long ct = v;
    changed.clear();
    changed.resize(n, false);
    for (long long i = 1; i < m; i++) {
      if (util[i].size() < k) continue;
      for (long long j = 0; j < util[i].size() - (k - 1); j++) {
        ct++;
        temp += util[i][j].first;
        changed[util[i][j].second] = true;
      }
    }
    if (ct > k) continue;
    for (long long i = 0; i < other.size(); i++) {
      if (ct == k) break;
      if (changed[other[i].second]) continue;
      temp += other[i].first;
      ct++;
    }
    if (res == -1) res = temp;
    res = min(res, temp);
  }
  cout << res << endl;
  return 0;
}
