#include <bits/stdc++.h>
using namespace std;
const int N = 3007;
vector<int> a[N];
int main() {
  int n, m;
  cin >> n >> m;
  multiset<int> stt;
  for (int i = 0; i < n; i++) {
    int id, c;
    cin >> id >> c;
    a[id].push_back(c);
    if (id != 1) stt.insert(c);
  }
  for (int i = 1; i <= m; i++) sort(a[i].begin(), a[i].end());
  long long ans = 1e18 + 7;
  for (int tar = 1; tar <= n; tar++) {
    multiset<int> st = stt;
    int v = a[1].size();
    long long cost = 0;
    for (int i = 2; i <= m; i++) {
      int fu = a[i].size();
      for (int j = 0; j < fu - tar + 1; j++) {
        int cc = a[i][j];
        cost += cc;
        st.erase(st.find(cc));
        v++;
      }
    }
    for (auto it = st.begin(); it != st.end() && v < tar; it++, v++)
      cost += *it;
    if (v < tar) continue;
    ans = min(ans, cost);
  }
  cout << ans << endl;
}
