#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C& a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C& a4, C b4) {
  a4 = max(a4, b4);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> pair) {
  return out << "(" << pair.first << ", " << pair.second << ")";
}
const int maxn = 3010;
const long long INF = 1LL << 62;
int c[maxn], idx;
int ph[maxn];
int pc[maxn];
vector<vector<long long> > ps;
vector<pair<int, int> > q;
int n, m;
long long fun(int x) {
  memset(pc, 0, sizeof pc);
  int fh = ps[0].size();
  long long c = 0;
  for (int i = (1); i < (m); i++) {
    int h = ps[i].size();
    int d = h - x;
    if (d >= 0) {
      pc[i] += d + 1;
      c += ps[i][d];
      fh += d + 1;
    }
  }
  int missing = max(0, x - fh);
  int k = 0;
  while (missing) {
    if (pc[q[k].second] > 0) {
      pc[q[k].second]--;
    } else {
      c += q[k].first;
      missing--;
    }
    k++;
  }
  return c;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  ps.assign(m, vector<long long>());
  for (int i = (0); i < (n); i++) {
    cin >> idx >> c[i];
    idx--;
    ps[idx].push_back(c[i]);
    if (idx > 0) q.push_back(pair<int, int>(c[i], idx));
  }
  sort(q.begin(), q.end());
  for (int i = (0); i < (m); i++) {
    sort(ps[i].begin(), ps[i].end());
  }
  for (int i = (0); i < (m); i++) {
    for (int j = (0); j < (ps[i].size()); j++) {
      if (j > 0) ps[i][j] += ps[i][j - 1];
    }
  }
  long long mini = INF;
  for (int i = (1); i < (n + 1); i++) {
    mini = min(mini, fun(i));
  }
  cout << mini << '\n';
  return 0;
}
