#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e5 + 20;
const long long Inf = 2e15 + 20;
long long mark[inf], v[inf];
long long mi = Inf;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, i, j;
  cin >> n >> m;
  pair<long long, long long> p[n];
  for (i = 0; i < n; ++i) cin >> p[i].second >> p[i].first;
  sort(p, p + n);
  for (i = 1; i <= (n / 2) + 1; ++i) {
    fill(mark, mark + n + 5, 0);
    fill(v, v + m + 5, 0);
    long long ans = 0;
    for (j = 0; j < n; ++j) v[p[j].second]++;
    for (j = 0; j < n; ++j) {
      if (p[j].second > 1 && v[p[j].second] >= i) {
        v[p[j].second]--;
        v[1]++;
        ans += p[j].first;
        mark[j] = 1;
      }
    }
    for (j = 0; j < n; ++j) {
      if (v[1] < i && p[j].second > 1 && !mark[j]) {
        v[p[j].second]--;
        v[1]++;
        ans += p[j].first;
        mark[j] = 1;
      }
    }
    mi = min(ans, mi);
  }
  cout << mi;
  return 0;
}
