#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:367772116")
using namespace std;
vector<int> len;
int n, m;
vector<pair<long long, int>> othergolos;
long long hzche(long long want) {
  long long other = want - len[0];
  vector<long long> need(m);
  for (size_t i = 1; i < m; i++) {
    if (len[i] < want) continue;
    other -= len[i] - want + 1;
    need[i] = len[i] - want + 1;
  }
  long long ans = 0;
  for (size_t i = 0; i < othergolos.size(); i++) {
    if (need[othergolos[i].second]) {
      ans += othergolos[i].first;
      need[othergolos[i].second]--;
      continue;
    }
    if (other > 0) {
      ans += othergolos[i].first;
      other--;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  len.resize(m);
  for (size_t i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    len[a]++;
    if (a) othergolos.push_back({b, a});
  }
  sort(othergolos.begin(), othergolos.end());
  long long ans = LLONG_MAX;
  for (size_t i = 0; i <= n; i++) {
    auto buflen = len;
    long long ret = hzche(i);
    ans = min(ans, ret);
    len = buflen;
  }
  cout << ans;
}
