#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > voters[3003];
vector<pair<long long, long long> > all;
bool taken[3003];
long long ans = LLONG_MAX;
bool solve(long long x, long long m, long long n) {
  long long tc = 0, tv = voters[1].size();
  for (long long i = 1; i <= n; ++i) {
    taken[i] = false;
  }
  for (long long i = 2; i <= m; ++i) {
    if (voters[i].size() >= x) {
      for (long long j = 0; j <= voters[i].size() - x; ++j) {
        if (j == voters[i].size()) break;
        tc += voters[i][j].first;
        taken[voters[i][j].second] = true;
        tv++;
      }
    }
  }
  for (auto p : all) {
    if (tv >= x) break;
    if (!taken[p.second]) {
      tv++;
      tc += p.first;
    }
  }
  if (tv > x)
    return false;
  else {
    ans = min(ans, tc);
    return true;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; ++i) {
    long long v, c;
    cin >> v >> c;
    voters[v].push_back(make_pair(c, i + 1));
    if (v != 1) all.push_back(make_pair(c, i + 1));
  }
  sort(all.begin(), all.end());
  for (long long i = 1; i <= m; ++i) {
    sort(voters[i].begin(), voters[i].end());
  }
  for (long long i = 0; i <= n; ++i) {
    long long is = solve(i, m, n);
  }
  cout << ans << endl;
}
