#include <bits/stdc++.h>
using namespace std;
struct vote {
  int num;
  int cost;
  int party;
} v[5000];
int cmp(vote a, vote b) { return a.cost < b.cost; }
int book[5000];
int main() {
  int n, m;
  cin >> n >> m;
  int i;
  int maxx = 0;
  int amount = 0;
  int now = 0;
  map<int, int> mp;
  map<int, int> mp2;
  for (i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    if (a == 1)
      now++;
    else {
      v[++amount].party = a;
      v[amount].cost = b;
      v[amount].num = amount;
      mp[a]++;
      maxx = max(mp[a], maxx);
    }
  }
  for (i = 2; i <= m; i++) mp2[mp[i]]++;
  for (i = n; i >= 1; i--) mp2[i] += mp2[i + 1];
  int j, k;
  long long minx = 1e15;
  if (now > maxx) {
    cout << 0;
    return 0;
  }
  int now2 = 0;
  int sum2 = 0;
  sort(v + 1, v + amount + 1, cmp);
  for (i = maxx + 1; i >= 1; i--) {
    if (now > i) break;
    int j;
    long long sum = 0;
    sum2 += mp2[i];
    now2 = i - sum2 - now;
    if (now2 < 0) break;
    int cost = 0;
    int sum3 = 0;
    memset(book, 0, sizeof(book));
    for (j = 1; j <= amount; j++) {
      if (mp[v[j].party] - book[v[j].party] >= i) {
        sum += v[j].cost;
        book[v[j].party]++;
      } else if (sum3 < now2) {
        sum += v[j].cost;
        sum3++;
      }
    }
    minx = min(minx, sum);
  }
  cout << minx;
}
