#include <bits/stdc++.h>
using namespace std;
struct Person {
  int id;
  int party;
  long long coin;
  bool isUsed;
};
Person persons[3010];
struct Party {
  int id;
  int count;
  std::vector<Person> personCoin;
};
Party parties[3010];
int n, m;
bool compareNum(const Person &a, const Person &b) { return a.coin > b.coin; }
bool compare(const Person &a, const Person &b) {
  if (a.coin == b.coin) {
    return parties[a.party].count > parties[b.party].count;
  } else
    return a.coin < b.coin;
}
long long totalCoin;
std::vector<int> totalParty;
int getMax() {
  int max = -2;
  int maxId;
  int maxCount = 0;
  for (int i = 2; i <= m; i++) {
    if (parties[i].count > max) {
      totalParty.clear();
      max = parties[i].count;
      maxId = i;
      maxCount = 1;
      totalParty.push_back(i);
    } else if (parties[i].count == max) {
      maxCount++;
      totalParty.push_back(i);
    }
  }
  if (maxCount >= 2)
    return -1;
  else
    return maxId;
}
int main() {
  scanf("%d%d", &n, &m);
  int total;
  int p = 0;
  for (int ii = 0; ii < n; ii++) {
    scanf("%d%I64d", &(persons[p].party), &(persons[p].coin));
    parties[persons[p].party].count++;
    if (persons[p].party != 1) {
      p++;
    }
  }
  if (p == 0) {
    printf("0\n");
    return 0;
  }
  int szPersons = p;
  sort(persons, persons + szPersons, compare);
  for (int i = 0; i < szPersons; i++) {
    persons[i].isUsed = false;
    persons[i].id = i;
    parties[persons[i].party].personCoin.push_back(persons[i]);
  }
  for (int i = 1; i <= m; i++)
    sort(parties[i].personCoin.begin(), parties[i].personCoin.end(),
         compareNum);
  totalCoin = 0;
  int curMax;
  int qa = 0, qb = 1;
  while (1) {
    curMax = getMax();
    if (parties[1].count > parties[totalParty[0]].count) break;
    long long curMaxCount = parties[totalParty[0]].count;
    long long needLess = 0;
    for (int i = 0; i < totalParty.size(); i++) {
      int sz = parties[totalParty[i]].personCoin.size();
      needLess = needLess + parties[totalParty[i]].personCoin[sz - 1].coin;
    }
    int needCount = curMaxCount - parties[1].count + 1;
    if (needCount > totalParty.size() + 1) needCount = totalParty.size() + 1;
    long long needRound = 0;
    int curCount = 0;
    std::vector<int> roundCount;
    for (int i = qa; i < n; i++) {
      if (persons[i].isUsed) continue;
      needRound = needRound + persons[i].coin;
      roundCount.push_back(i);
      curCount++;
      if (curCount >= needCount) break;
    }
    if (needLess < needRound) {
      totalCoin = totalCoin + needLess;
      parties[1].count = parties[1].count + totalParty.size();
      int q;
      int sz;
      for (int i = 0; i < totalParty.size(); i++) {
        q = totalParty[i];
        sz = parties[q].personCoin.size();
        persons[parties[q].personCoin[sz - 1].id].isUsed = true;
        parties[q].count--;
        parties[q].personCoin.pop_back();
      }
    } else {
      int count = roundCount.size();
      if (count > 1) count--;
      for (int i = 0; i < count; i++) {
        qa = roundCount[i];
        totalCoin = totalCoin + persons[qa].coin;
        parties[1].count++;
        persons[qa].isUsed = true;
        parties[persons[qa].party].count--;
        parties[persons[qa].party].personCoin.pop_back();
      }
    }
    while (persons[qa].isUsed) qa++;
  }
  printf("%I64d\n", totalCoin);
  return 0;
}
