#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long>> pq1, pq2;
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    res *= a;
    b--;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, x, y, l, h;
  vector<long long> v[30005];
  vector<long long> w;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  l = v[1].size();
  h = n;
  for (int i = 1; i < m + 1; ++i) {
    if (v[i].size() > 0) sort(v[i].begin(), v[i].end());
  }
  long long mn = 1000000000000000000;
  if (l == 0) l++;
  for (int i = l; i < h + 1; ++i) {
    long long ans = 0, cnt = v[1].size();
    for (int j = 2; j < m + 1; ++j) {
      long long p1 = 0;
      while (v[j].size() - p1 >= i) {
        ans += v[j][p1];
        cnt++;
        p1++;
      }
      while (p1 < v[j].size()) {
        w.push_back(v[j][p1]);
        p1++;
      }
    }
    if (cnt <= i) {
      long long st = i - cnt;
      if (w.size() > 0) sort(w.begin(), w.end());
      long long p = 0;
      while (p < st) {
        ans += w[p];
        p++;
      }
      if (ans <= mn) mn = ans;
    }
    w.clear();
  }
  cout << mn << endl;
  return 0;
}
