#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > data;
vector<pair<int, int> > p[3005];
int has;
bool used[3005];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (x == 1) has++;
    data.emplace_back(y, x);
  }
  sort(data.begin(), data.end());
  for (int i = 0; i < n; i++) {
    p[data[i].second].emplace_back(data[i].first, i);
  }
  long long ans = 1000000000LL * 1000000000LL;
  for (int voter = 0; voter <= n; voter++) {
    int cnt = has;
    long long cost = 0;
    memset(used, 0, 3005);
    for (int i = 2; i <= m; i++) {
      for (int j = 0; j < (int)p[i].size() - voter; j++) {
        cost += p[i][j].first;
        used[p[i][j].second] = 1;
        cnt++;
      }
    }
    for (int i = 0; i < n && cnt <= voter; i++) {
      if (used[i] || data[i].second == 1) continue;
      cnt++;
      cost += data[i].first;
    }
    if (cnt > voter) ans = min(ans, cost);
  }
  cout << ans;
}
