#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int cnt[maxn];
vector<int> a[maxn];
int p[maxn], c[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &p[i], &c[i]);
    a[p[i]].push_back(c[i]);
    cnt[p[i]]++;
  }
  for (int i = 1; i <= m; i++) {
    sort(a[i].begin(), a[i].end());
    reverse(a[i].begin(), a[i].end());
  }
  long long ans = 1e18;
  for (int i = 1; i <= n; i++) {
    long long tmp = 0;
    int c = 0;
    vector<int> can;
    for (int j = 2; j <= m; j++) {
      int t = min(i - 1, (int)a[j].size());
      for (int k = 0; k < t; k++) can.push_back(a[j][k]);
      for (int k = t; k < (int)a[j].size(); k++) {
        tmp += a[j][k];
        c++;
      }
    }
    sort(can.begin(), can.end());
    for (int j = 0; j < i - cnt[1] - c; j++) tmp += can[j];
    ans = min(ans, tmp);
  }
  cout << ans;
  return 0;
}
