#include <bits/stdc++.h>
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 5;
using namespace std;
int t, n, m, x, y, z;
vector<int> v[3005];
long long check(int x) {
  long long sum = 0;
  int cnt = v[1].size();
  for (int i = 2; i <= m; i++) {
    if (v[i].size() >= x) {
      int ans = v[i].size() - x + 1;
      for (int j = 0; j < ans; j++) {
        sum += 1ll * v[i][j];
      }
      cnt += ans;
    }
  }
  if (cnt < x) {
    vector<int> vv;
    for (int i = 2; i <= m; i++) {
      if (v[i].size() < x) {
        for (int j = 0; j < v[i].size(); j++) vv.push_back(v[i][j]);
      } else {
        int ans = v[i].size() - x + 1;
        for (int j = ans; j < v[i].size(); j++) {
          vv.push_back(v[i][j]);
        }
      }
    }
    sort(vv.begin(), vv.end());
    for (int i = 0; cnt < x; i++, cnt++) {
      sum += 1ll * vv[i];
    }
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
  }
  for (int i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
  }
  long long minn = inf;
  for (int i = 1; i <= n / 2 + 1; i++) {
    minn = min(minn, check(i));
  }
  printf("%lld\n", minn);
  return 0;
}
