#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m;
    b /= 2;
    a = ((a % m) * (a % m)) % m;
  }
  return ans % m;
}
long long n, m;
long long cnt[3005];
set<pair<long long, long long> > g[3005];
long long c[3005];
long long p[3005];
long long solve(long long x) {
  long long ans = cnt[1];
  long long sum = 0;
  set<int> vis;
  priority_queue<long long, vector<long long>, greater<long long> > pq;
  for (long long i = 2; i <= m; i++) {
    if (g[i].size() < x) continue;
    long long k = g[i].size() - x + 1;
    long long t = k;
    for (set<pair<long long, long long> >::iterator it = g[i].begin();
         it != g[i].end(); it++) {
      if (k == 0) break;
      sum += (*it).first;
      vis.insert((*it).second);
      k--;
    }
    ans += t;
  }
  for (long long i = 1; i <= n; i++) {
    if (p[i] == 1) continue;
    if (vis.find(i) == vis.end()) {
      pq.push(c[i]);
    }
  }
  while (ans < x && !pq.empty()) {
    sum += pq.top();
    pq.pop();
    ans++;
  }
  if (ans < x) {
    return (long long)1e18;
  }
  return sum;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> p[i] >> c[i];
    g[p[i]].insert(make_pair(c[i], i));
    cnt[p[i]]++;
  }
  long long ans = (long long)1e18;
  for (long long i = max(1LL, cnt[1]); i <= n; i++) {
    ans = min(ans, solve(i));
  }
  cout << ans;
}
