#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, tmp;
  long long Hest = 0;
  cin >> N >> M;
  vector<long long> party[M + 1];
  for (int i = 0; i < N; i++) {
    long long r, c;
    cin >> r >> c;
    party[r].push_back(c);
  }
  for (int i = 2; i <= M; i++) {
    sort(party[i].begin(), party[i].end());
    Hest = max(Hest, (long long)party[i].size());
    for (int j = 1; j < party[i].size(); j++) {
      party[i][j] += party[i][j - 1];
    }
  }
  long long ans = 10000000000000007;
  for (int i = 0; i <= Hest; i++) {
    int mine = party[1].size();
    int loc[M + 1];
    long long now = 0;
    priority_queue<pair<int, int>, vector<pair<int, int> >,
                   greater<pair<int, int> > >
        PQ;
    for (int j = 2; j <= M; j++) {
      if (party[j].empty()) continue;
      tmp = party[j].size();
      if (tmp > i) {
        mine += tmp - i;
        tmp -= i + 1;
        loc[j] = tmp + 1;
        now += party[j][tmp];
        PQ.push(make_pair(party[j][tmp + 1] - party[j][tmp], j));
      } else {
        loc[j] = 0;
        PQ.push(make_pair(party[j][0], j));
      }
    }
    while (mine <= i) {
      mine++;
      now += PQ.top().first;
      if (loc[PQ.top().second] + 1 < party[PQ.top().second].size()) {
        PQ.push(make_pair(party[PQ.top().second][loc[PQ.top().second] + 1] -
                              party[PQ.top().second][loc[PQ.top().second]],
                          PQ.top().second));
        loc[PQ.top().second]++;
      }
      PQ.pop();
    }
    ans = min(ans, now);
  }
  cout << ans << endl;
}
