#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long INF64 = 1e18;
const long double EPS = 1e-9;
int n, m, p, c, berland;
long long ans = 1e12;
vector<vector<int> > party_votes;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  party_votes.resize(m + 1);
  for (int i = 0; i != int(n); ++i) {
    cin >> p >> c;
    berland += (p == 1);
    party_votes[p].push_back(c);
  }
  for (int i = int(2); i != int(m + 1); ++i)
    sort(party_votes[i].begin(), party_votes[i].end());
  for (int i = int(1); i != int(n + 1); ++i) {
    int cur = berland;
    long long cost = 0;
    priority_queue<int, vector<int>, greater<int> > pq;
    for (int j = int(2); j != int(m + 1); ++j) {
      int sz = party_votes[j].size();
      int k = 0;
      while (sz - k >= i && k != sz) {
        cost += party_votes[j][k++];
        cur++;
      }
      while (k != sz) pq.push(party_votes[j][k++]);
    }
    while (cur < i) {
      cost += pq.top();
      cur++;
      pq.pop();
    }
    ans = min(ans, cost);
  }
  cout << ans << endl;
}
