#include <bits/stdc++.h>
using namespace std;
class CElections {
 public:
  struct person {
    long long p, c;
  };
  int n, m;
  person arr[3100];
  vector<long long> parties[3100];
  long long sol(int votes) {
    int total_votes_needed = votes - (int)parties[0].size();
    vector<long long> rem;
    long long ans = 0;
    for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
         i += 1 - 2 * ((0) > (m))) {
      if (!i) continue;
      int num = (int)parties[i].size() - votes + 1;
      if (num > 0) {
        total_votes_needed -= num;
        for (__typeof(num) j = (0) - ((0) > (num)); j != (num) - ((0) > (num));
             j += 1 - 2 * ((0) > (num)))
          ans += parties[i][j];
      }
      for (int j = max(num, 0); j < parties[i].size(); j++)
        rem.push_back(parties[i][j]);
    }
    if (total_votes_needed > 0) {
      nth_element(rem.begin(), rem.begin() + total_votes_needed, rem.end());
      for (__typeof(total_votes_needed) i = (0) - ((0) > (total_votes_needed));
           i != (total_votes_needed) - ((0) > (total_votes_needed));
           i += 1 - 2 * ((0) > (total_votes_needed)))
        ans += rem[i];
    }
    return ans;
  }
  void solve(std::istream &in, std::ostream &out) {
    in >> n >> m;
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      in >> arr[i].p >> arr[i].c, arr[i].p--,
          parties[arr[i].p].push_back(arr[i].c);
    sort(arr, arr + n, [](person a, person b) { return a.c < b.c; });
    for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
         i += 1 - 2 * ((0) > (m)))
      sort(parties[i].begin(), parties[i].end());
    int low = max(1, (int)parties[0].size()), high = n;
    while (low < high) {
      int mid = (low + high) / 2;
      if (sol(mid) < sol(mid + 1)) {
        high = mid;
      } else {
        low = mid + 1;
      }
    }
    long long ans = sol(low);
    out << ans << '\n';
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  CElections solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
