#include <bits/stdc++.h>
using namespace std;
signed main() {
  std::ios_base::sync_with_stdio(0), cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long>>> g(m);
  vector<bool> used(n);
  vector<pair<long long, long long>> all;
  for (long long i = 0; i < n; ++i) {
    long long p, c;
    cin >> p >> c;
    p--;
    g[p].push_back({c, i});
    all.push_back({c, i});
  }
  sort(all.begin(), all.end());
  for (long long i = 0; i < m; ++i) sort(g[i].begin(), g[i].end());
  long long ans = 3e18;
  for (long long i = max(1ll, (long long)g[0].size()); i <= n; ++i) {
    used.assign(n, 0);
    for (long long j = 0; j < (long long)g[0].size(); ++j)
      used[g[0][j].second] = 1;
    long long cnt = g[0].size();
    long long cur = 0;
    for (long long j = 1; j < m; ++j) {
      if (g[j].size() >= i) {
        long long k = (long long)g[j].size() - i + 1;
        for (long long h = 0; h < k; ++h) {
          cur += 1ll * g[j][h].first;
          used[g[j][h].second] = 1;
          cnt++;
        }
      }
    }
    if (cnt < i) {
      for (long long j = 0; j < n && cnt < i; ++j) {
        if (!used[all[j].second]) {
          cnt++;
          cur += 1ll * all[j].first;
        }
      }
    }
    if (cnt >= i) ans = min(ans, cur);
  }
  cout << ans;
  return 0;
}
